﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMisuseAn.behaviour.model;
using PMisuseAn.behaviour.adaptation;

namespace PMisuseAn.io
{
    public class MisUseAnObjectsDB
    {
        //private static List<string> processed_relations;

        private static MisuseAnDataContext db = new MisuseAnDataContext();

        public static List<string> loadActorNames(string clusterName)
        {
            List<string> actors_list = ActorSerialisationEngine.getActorsList(clusterName);

            //List<string> actors_list = new List<string>();

            //var actors = (from actor in db.ActorDBs
            //              where actor.clusterName == clusterName
            //              select actor.actorName).Distinct();

            //foreach (var actor in actors)
            //{
            //    actors_list.Add(actor);
            //}

            return actors_list;
        }

        public static Actor loadActor(string actorName, string clusterName)
        {
            ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
            if (actorDB == null)
                return null;

            Actor actor = actorDB.actor;

            //Actor actor = new Actor();
            //actor.name = actorName;
            //actor.clusterName = clusterName;

            //List<Attribute> attributes = loadAttributes(actorName, clusterName);            
            //actor.setAttributes(attributes);

            //List<string> roles = loadActorRoles(actorName, clusterName);
            //actor.setRoles(roles);

            return actor;
        }

        public static List<Actor> loadAllActorsFull(string clusterName)
        {
            List<Actor> actors = new List<Actor>();
            List<string> actorNames = loadActorNames(clusterName);

            foreach (string actorName in actorNames)
            {
                Actor actor = loadActor(actorName, clusterName);
                actors.Add(actor);
            }
            return actors;
        }

        public static void addActor(Actor actor, string clusterName)
        {
            foreach (Attribute att in actor.attributes)
            {
                addActorDB(att, actor.name, clusterName);
            }

            foreach (string role in actor.roles)
            {
                addActorRole(role, actor.name, clusterName);
            }
        }

        public static void deleteActor(string actorName, string clusterName)
        {
            deleteActorRoles(actorName, clusterName);
            //deleteGroupAgentDBActorType(actorName, clusterName);

            ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

            //var actor_dbs =
            //(from actor in db.ActorDBs
            // where actor.actorName == actorName && actor.clusterName == clusterName
            // select actor);
            //foreach (ActorDB actor_db in actor_dbs)
            //{
            //    db.ActorDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}            
        }

        private static void deleteActorRoles(string actorName, string clusterName)
        {
            Actor actor = loadActor(actorName, clusterName);
            if (actor == null)
                return;
            ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

            actor.setRoles(new List<string>());
            ActorDB actorDB = new ActorDB(actor);
            ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);


            //ActorRoleSerialisationEngine.deleteActorRoleDB(actorName, clusterName);

            //var actor_dbs =
            //(from actor in db.ActorRoleDBs
            // where actor.actorName == actorName 
            //   && actor.clusterName == clusterName
            // select actor);

            //foreach (ActorRoleDB actor_db in actor_dbs)
            //{
            //    db.ActorRoleDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }

        //TODO: disable multiple roles
        public static void deleteActorRole(string actorName, string roleName, string clusterName)
        {
            Actor actor = loadActor(actorName, clusterName);
            if (actor != null)
            {
                ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

                foreach (string role in actor.roles)
                {
                    if (role.Equals(roleName))
                    {
                        actor.removeRole(roleName);
                        break;
                    }
                }
                ActorDB actorDB = new ActorDB(actor);
                ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);

            }


            //var actor_dbs =
            //(from actor in db.ActorRoleDBs
            // where actor.actorName == actorName
            //   && actor.clusterName == clusterName
            //   && actor.roleName == roleName
            // select actor).SingleOrDefault<ActorRoleDB>() ;

            //if (actor_dbs != null)
            //{
            //    db.ActorRoleDBs.DeleteOnSubmit(actor_dbs);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteActor(string actorName, string attributeName, string clusterName)
        {
            //deleteActorRoles(actorName, clusterName);
            //deleteGroupAgentDBActorType(actorName, clusterName);

            Actor actor = loadActor(actorName, clusterName);
            if (actor != null)
            {
                ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

                foreach (Attribute att in actor.attributes)
                {
                    if (att.name.Equals(attributeName))
                    {
                        actor.attributes.Remove(att);
                        break;
                    }
                }
                ActorDB actorDB = new ActorDB(actor);
                ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);
            }

            //var actor_dbs =
            //(from actor in db.ActorDBs
            // where actor.actorName == actorName
            // && actor.actorAttribute == attributeName
            // && actor.clusterName == clusterName
            // select actor);
            //foreach (ActorDB actor_db in actor_dbs)
            //{
            //    db.ActorDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteAggregationStatement(string statement, string clusterName)
        {
            List<string> aggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string aggName in aggNames)
            {
                AttributeAggregationStatementDB attAggStat =
                    AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, aggName);
                if (attAggStat != null)
                {
                    if (attAggStat.aggregationStatement.Equals(statement))
                    {
                        AttributeAggregationStatementSerialisationEngine.deleteAttributeAggregationDB(aggName, clusterName);
                        break;
                    }
                }
            }

            //var att_agg_dbs =
            //(from agg_statement in db.AttributeAggregationStatementDBs
            // where agg_statement.aggregationStatement == statement
            // && agg_statement.clusterName == clusterName
            // select agg_statement);

            //foreach (AttributeAggregationStatementDB stat_db in att_agg_dbs)
            //{
            //    db.AttributeAggregationStatementDBs.DeleteOnSubmit(stat_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deletePolicyStatement(string statement, string transmissionPrinciple, string clusterName)
        {
            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementDB policyStat =
                    PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyName);
                if (policyStat != null)
                {
                    if (policyStat.policyStatement.Equals(statement))
                    {
                        PolicyStatementSerialisationEngine.deletePolicyStatementDB(policyName, clusterName);
                        break;
                    }
                }
            }

            //var policy_dbs =
            //(from p_statement in db.PolicyStatementDBs
            // where p_statement.policyStatement == statement
            // && p_statement.clusterName == clusterName
            // && p_statement.transmissionPrinciple == transmissionPrinciple
            // select p_statement);

            //foreach (PolicyStatementDB stat_db in policy_dbs)
            //{
            //    int policyID = stat_db.policyID;
            //    //deleteStateTransitionDBPolicy(policyID, clusterName);
            //    updateStateTransitionDBPolicy(policyID, clusterName);
            //    db.PolicyStatementDBs.DeleteOnSubmit(stat_db);
            //    db.SubmitChanges();
            //}
        }

        private static void updateActorDB(string actorName, string attName, string isPrivateAttribute, string clusterName)
        {
            Actor actor = loadActor(actorName, clusterName);
            if (actor != null)
            {
                ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

                Attribute att = loadAttributeFull(attName, clusterName);
                actor.addAttribute(att);

                ActorDB actorDB = new ActorDB(actor);
                ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);
            }

            //var act = (from act_ in db.ActorDBs
            //           where act_.actorName == actorName
            //           && act_.actorAttribute == attName
            //           && act_.clusterName == clusterName
            //           select act_).SingleOrDefault<ActorDB>();
            //if (act != null)
            //{
            //    act.actorName = actorName;
            //    act.actorAttribute = attName;
            //    act.isPrivateAttribute = isPrivateAttribute;
            //    act.clusterName = clusterName;
            //    db.SubmitChanges();
            //}
        }

        private static void addActorRole(string roleName, string actorName, string clusterName)
        {
            Actor actor = loadActor(actorName, clusterName);
            if (actor == null)
                return;
            ActorSerialisationEngine.deleteActorDB(actorName, clusterName);

            actor.addRole(roleName);

            ActorDB actorDB = new ActorDB(actor);
            ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);

            //if (findActorRole(actorName, roleName, clusterName) == null)
            //{
            //    ActorRoleDB actor_db = new ActorRoleDB();
            //    actor_db.actorName = actorName;
            //    actor_db.roleName = roleName;
            //    actor_db.clusterName = clusterName;

            //    db.ActorRoleDBs.InsertOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }

        private static void addActorDB(Attribute att, string actorName, string clusterName)
        {
            Actor actor = loadActor(actorName, clusterName);
            if (actor == null)
            {
                actor = new Actor();
                actor.name = actorName;
                actor.clusterName = clusterName;
                actor.addAttribute(att);

                ActorDB actorDB = new ActorDB(actor);
                ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);

            }
            else
            {
                actor.addAttribute(att);
                ActorDB actorDB = new ActorDB(actor);
                ActorSerialisationEngine.serialise(actor.name, actor.clusterName, actorDB);
            }

            //if (findActorAttribute(actorName, att.name, clusterName) == null)
            //{
            //    ActorDB actor_db = new ActorDB();
            //    actor_db.actorName = actorName;
            //    actor_db.actorAttribute = att.name;
            //    actor_db.clusterName = clusterName;

            //    if (att.isPrivate == true)
            //    {
            //        actor_db.isPrivateAttribute = "true";
            //    }
            //    else
            //    {
            //        actor_db.isPrivateAttribute = "false";
            //    }
            //    addAttributeDB(att, actorName, clusterName);

            //    db.ActorDBs.InsertOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    string attp = att.isPrivate+"";
            //    string attn = att.name;
            //    updateActorDB(actorName, attn, attp, clusterName);
            //}
        }

        private static bool attExist(Attribute att, string clusterName)
        {
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);
            bool exist = false;
            foreach (string attName in attributeNames)
            {
                if (attName.Equals(att.name))
                {
                    exist = true;
                    break;
                }
            }

            return exist;

            //int count_att = (from attribute_ in db.AttributeDBs
            //           where attribute_.attributeName == att.name
            //           && attribute_.clusterName == clusterName
            //                 select attribute_).Distinct().Count();

            //if (count_att > 0)
            //    return true;
            //else
            //    return false;
        }

        public static void updateAttributeDB(Attribute u_attribute, string clusterName)
        {
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);
            foreach (string attName in attributeNames)
            {
                if (attName.Equals(u_attribute.name))
                {
                    AttributeSerialisationEngine.deleteAttributeDB(attName, clusterName);

                    AttributeDB attDB = new AttributeDB(u_attribute);
                    AttributeSerialisationEngine.serialise(attName, clusterName, attDB);

                    break;
                }
            }

            //var att = (from attribute_ in db.AttributeDBs
            //           where attribute_.attributeName == u_attribute.name
            //           && attribute_.clusterName == clusterName
            //           select attribute_).SingleOrDefault<AttributeDB>();
            //if (att != null)
            //{
            //    att.attributeName = u_attribute.name;
            //    att.attributeLiteralType = u_attribute.literalType;
            //    att.attributeValue = u_attribute.attributeValue;
            //    att.clusterName = clusterName;
            //    if (u_attribute.personallyIdentifiable == false)
            //    {
            //        att.personallyIdentifiable = "false"; ;

            //    }
            //    else
            //    {
            //        att.personallyIdentifiable = "true";
            //    }
            //    att.category = u_attribute.category;
            //    att.description = u_attribute.description;
            //    db.SubmitChanges();
            //}
        }

        public static void addAttributeDB(Attribute s_attribute, string clusterName)
        {
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);
            bool exist = false;
            foreach (string attName in attributeNames)
            {
                if (attName.Equals(s_attribute.name))
                {
                    exist = true;
                    AttributeSerialisationEngine.deleteAttributeDB(attName, clusterName);

                    AttributeDB attDB = new AttributeDB(s_attribute);
                    AttributeSerialisationEngine.serialise(attName, clusterName, attDB);

                    break;
                }
            }

            if (exist == false)
            {
                AttributeDB attDB = new AttributeDB(s_attribute);
                AttributeSerialisationEngine.serialise(s_attribute.name, clusterName, attDB);
            }
            foreach (string att_agg_s in s_attribute.aggregation_statements)
            {
                addAggregationStatement(att_agg_s, clusterName);
            }
            //foreach (string att_domain in s_attribute.domain)
            //{
            //    addAttributeDomain(s_attribute, att_domain, clusterName);
            //}

            //AttributeDB att_db = new AttributeDB();
            //att_db.attributeName = s_attribute.name;
            //att_db.attributeLiteralType = s_attribute.literalType;
            //att_db.category = s_attribute.category;
            //att_db.description = s_attribute.description;
            //att_db.clusterName = clusterName;
            //att_db.attributeValue = s_attribute.attributeValue;

            //if (s_attribute.personallyIdentifiable == true)
            //{
            //    att_db.personallyIdentifiable = "true";
            //}
            //else
            //{
            //    att_db.personallyIdentifiable = "false";
            //}

            //Attribute att_exist = findAttribute(s_attribute.name, clusterName);
            //if (att_exist == null)
            //{
            //    db.AttributeDBs.InsertOnSubmit(att_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateAttributeDB(s_attribute, clusterName);
            //}

            //foreach (Attribute att_imp_from in s_attribute.implied_from)
            //{
            //    addAttributeImpliedFrom(s_attribute, att_imp_from, clusterName);
            //}
            //foreach (Attribute att_imp_to in s_attribute.implied_to)
            //{
            //    addAttributeImpliedTo(s_attribute, att_imp_to, clusterName);
            //}
            //foreach (Attribute att_sym in s_attribute.symmetry)
            //{
            //    addAttributeSymmetry(s_attribute, att_sym, clusterName);
            //}
            //foreach (string att_agg_s in s_attribute.aggregation_statements)
            //{
            //    addAggregationStatement(att_agg_s, clusterName);
            //}
            //foreach (string att_domain in s_attribute.domain)
            //{
            //    addAttributeDomain(s_attribute, att_domain, clusterName);
            //}
            //foreach (string att_type_of in s_attribute.type_of)
            //{
            //    addAttributeTypeOf(s_attribute.name, att_type_of, clusterName);
            //}
        }

        private static void addAttributeDB(Attribute att, string actorName, string clusterName)
        {
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);
            bool exist = false;
            foreach (string attName in attributeNames)
            {
                if (attName.Equals(att.name))
                {
                    exist = true;
                    AttributeSerialisationEngine.deleteAttributeDB(attName, clusterName);

                    AttributeDB attDB = new AttributeDB(att);
                    AttributeSerialisationEngine.serialise(attName, clusterName, attDB);

                    break;
                }
            }

            if (exist == false)
            {
                AttributeDB attDB = new AttributeDB(att);
                AttributeSerialisationEngine.serialise(att.name, clusterName, attDB);
            }

            foreach (string att_agg_s in att.aggregation_statements)
            {
                addAggregationStatement(att_agg_s, clusterName);
            }
            //foreach (string att_domain in att.domain)
            //{
            //    addAttributeDomain(att, att_domain, clusterName);
            //}

            //AttributeDB att_db = new AttributeDB();
            //att_db.attributeName = att.name;
            //att_db.attributeLiteralType = att.literalType;
            //att_db.category = att.category;
            //att_db.description = att.description;
            //att_db.clusterName = clusterName;
            //att_db.attributeValue = att.attributeValue;
            //if (att.personallyIdentifiable == true)
            //{
            //    att_db.personallyIdentifiable = "true";
            //}
            //else
            //{
            //    att_db.personallyIdentifiable = "false";
            //}

            //Attribute att_exist = findAttribute(att.name, clusterName);
            //if (att_exist == null)
            //{
            //    db.AttributeDBs.InsertOnSubmit(att_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateAttributeDB(att, clusterName);
            //}

            //foreach (Attribute att_imp_from in att.implied_from)
            //{
            //    if (att_imp_from.isPrivate)
            //    {
            //        addActorDB(att_imp_from, actorName, clusterName);
            //    }
            //    addAttributeImpliedFrom(att, att_imp_from, actorName, clusterName);
            //}
            //foreach (Attribute att_imp_to in att.implied_to)
            //{
            //    if (att_imp_to.isPrivate)
            //    {
            //        addActorDB(att_imp_to, actorName, clusterName);
            //    }
            //    addAttributeImpliedTo(att, att_imp_to, actorName, clusterName);
            //}
            //foreach (Attribute att_sym in att.symmetry)
            //{
            //    if (att_sym.isPrivate)
            //    {
            //        addActorDB(att_sym, actorName, clusterName);
            //    }
            //    addAttributeSymmetry(att, att_sym, actorName, clusterName);
            //}
            //foreach (string att_agg_s in att.aggregation_statements)
            //{
            //    addAggregationStatement(att_agg_s, clusterName);
            //}
            //foreach (string att_domain in att.domain)
            //{
            //    addAttributeDomain(att, att_domain, clusterName);
            //}
            //foreach (string att_type_of in att.type_of)
            //{
            //    addAttributeTypeOf(att.name, att_type_of, clusterName);
            //}
        }

        //private static void addAttributeImpliedFrom(Attribute att, Attribute imp_from_att, string actorName, string clusterName)
        //{       
        //    addAttributeDB(imp_from_att, actorName, clusterName);

        //    if (att.name.Equals(imp_from_att.name))
        //    {
        //        return;
        //    }

        //    AttributeImpliedFromDB att_imp_from_db = new AttributeImpliedFromDB();
        //    att_imp_from_db.attributeName = att.name;
        //    att_imp_from_db.impliedFrom = imp_from_att.name;
        //    att_imp_from_db.clusterName = clusterName;

        //    if (findImpliedFromAttribute(att.name, imp_from_att.name, clusterName) == null)
        //    {
        //        db.AttributeImpliedFromDBs.InsertOnSubmit(att_imp_from_db);
        //        db.SubmitChanges();
        //    }            
        //}
        //private static void addAttributeImpliedFrom(Attribute att, Attribute imp_from_att, string clusterName)
        //{
        //    addAttributeDB(imp_from_att, clusterName);

        //    if (att.name.Equals(imp_from_att.name))
        //    {
        //        return;
        //    }

        //    AttributeImpliedFromDB att_imp_from_db = new AttributeImpliedFromDB();
        //    att_imp_from_db.attributeName = att.name;
        //    att_imp_from_db.impliedFrom = imp_from_att.name;
        //    att_imp_from_db.clusterName = clusterName;

        //    if (findImpliedFromAttribute(att.name, imp_from_att.name, clusterName) == null)
        //    {
        //        db.AttributeImpliedFromDBs.InsertOnSubmit(att_imp_from_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void addAttributeImpliedTo(Attribute att, Attribute imp_to_att, string actorName, string clusterName)
        //{
        //    addAttributeDB(imp_to_att, actorName, clusterName);

        //    if (att.name.Equals(imp_to_att.name))
        //    {
        //        return;
        //    }

        //    AttributeImpliedToDB att_imp_to_db = new AttributeImpliedToDB();
        //    att_imp_to_db.attributeName = att.name;
        //    att_imp_to_db.impliedTo = imp_to_att.name;
        //    att_imp_to_db.clusterName = clusterName;

        //    if (findImpliedToAttribute(att.name, imp_to_att.name, clusterName) == null)
        //    {
        //        db.AttributeImpliedToDBs.InsertOnSubmit(att_imp_to_db);
        //        db.SubmitChanges();
        //    }            
        //}
        //private static void addAttributeImpliedTo(Attribute att, Attribute imp_to_att, string clusterName)
        //{            
        //    addAttributeDB(imp_to_att, clusterName);

        //    if (att.name.Equals(imp_to_att.name))
        //    {
        //        return;
        //    }

        //    AttributeImpliedToDB att_imp_to_db = new AttributeImpliedToDB();
        //    att_imp_to_db.attributeName = att.name;
        //    att_imp_to_db.impliedTo = imp_to_att.name;
        //    att_imp_to_db.clusterName = clusterName;

        //    if (findImpliedToAttribute(att.name, imp_to_att.name, clusterName) == null)
        //    {
        //        db.AttributeImpliedToDBs.InsertOnSubmit(att_imp_to_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void addAttributeSymmetry(Attribute att, Attribute symmetry_att, string actorName, string clusterName)
        //{
        //    addAttributeDB(symmetry_att, actorName, clusterName);

        //    if (att.name.Equals(symmetry_att.name))
        //    {
        //        return;
        //    }

        //    AttributeSymmetryDB att_sym_db = new AttributeSymmetryDB();
        //    att_sym_db.attributeName = att.name;
        //    att_sym_db.symmetry = symmetry_att.name;
        //    att_sym_db.clusterName = clusterName;

        //    if (findSymmetryAttribute(att.name, symmetry_att.name, clusterName) == null)
        //    {
        //        db.AttributeSymmetryDBs.InsertOnSubmit(att_sym_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void addAttributeSymmetry(Attribute att, Attribute symmetry_att, string clusterName)
        //{
        //    addAttributeDB(symmetry_att, clusterName);

        //    if (att.name.Equals(symmetry_att.name))
        //    {
        //        return;
        //    }

        //    AttributeSymmetryDB att_sym_db = new AttributeSymmetryDB();
        //    att_sym_db.attributeName = att.name;
        //    att_sym_db.symmetry = symmetry_att.name;
        //    att_sym_db.clusterName = clusterName;

        //    if (findSymmetryAttribute(att.name, symmetry_att.name, clusterName) == null)
        //    {
        //        db.AttributeSymmetryDBs.InsertOnSubmit(att_sym_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void addAggregationStatement(string att_agg_s, string clusterName)
        {
            List<string> aggStatNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);

            bool exist = false;
            foreach (string aggStatName in aggStatNames)
            {
                AttributeAggregationStatementDB aaDB = AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, aggStatName);
                if (aaDB.aggregationStatement.Equals(att_agg_s))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == false)
            {
                AttributeAggregationStatementDB attAggStatDB = new AttributeAggregationStatementDB(att_agg_s);
                AttributeAggregationStatementSerialisationEngine.serialise(attAggStatDB, clusterName);
            }

            //AttributeAggregationStatementDB aas = new AttributeAggregationStatementDB();
            //aas.aggregationStatement = att_agg_s;
            //aas.clusterName = clusterName;

            //if (aggregateStatementExist(att_agg_s, clusterName) == false)
            //{
            //    db.AttributeAggregationStatementDBs.InsertOnSubmit(aas);
            //    db.SubmitChanges();
            //}            
        }

        public static void addPolicyStatement(string policy_s, string transmissionPrinciple, string clusterName)
        {
            List<string> policyStatNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);

            bool exist = false;
            foreach (string policyStatName in policyStatNames)
            {
                PolicyStatementDB aaDB = PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyStatName);
                if (aaDB.policyStatement.Equals(policy_s))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == false)
            {
                string pid = RandomEngine.GetUniqueName(10);
                PolicyStatementDB policyStatDB = new PolicyStatementDB(policy_s, transmissionPrinciple, pid);
                PolicyStatementSerialisationEngine.serialise(policyStatDB, clusterName);
            }

            //PolicyStatementDB ps = new PolicyStatementDB();
            //ps.policyStatement = policy_s;
            //ps.clusterName = clusterName;
            //ps.transmissionPrinciple = transmissionPrinciple;

            //if (policyStatementExist(policy_s, transmissionPrinciple, clusterName) == false)
            //{
            //    db.PolicyStatementDBs.InsertOnSubmit(ps);
            //    db.SubmitChanges();
            //}
        }

        public static void addAttributeDomain(Attribute att, string att_domain, string clusterName)
        {
            att.addDomain(att_domain);
            AttributeDB attributeDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, att.name);
            AttributeSerialisationEngine.deleteAttributeDB(att.name, clusterName);
            attributeDB.attribute = att;
            AttributeSerialisationEngine.serialise(att.name, clusterName, attributeDB);

            //ConstraintDB cdb = new ConstraintDB();
            //cdb.attributeName = att.name;
            //cdb.attributeDomainConstraint = att_domain;
            //cdb.clusterName = clusterName;

            //if (attributeDomainConstraintExist(att.name, att_domain, clusterName) == false)
            //{
            //    db.ConstraintDBs.InsertOnSubmit(cdb);
            //    db.SubmitChanges();
            //}
        }

        //public static void addAttributeTypeOf(string child_att, string parent_att, string clusterName)
        //{
        //    AttributeTypeOfDB atodb = new AttributeTypeOfDB();
        //    atodb.childAttributeName = child_att;
        //    atodb.parentAttributeName = parent_att;
        //    atodb.clusterName = clusterName;

        //    if (attributeTypeOfExist(child_att, parent_att, clusterName) == false)
        //    {
        //        db.AttributeTypeOfDBs.InsertOnSubmit(atodb);
        //        db.SubmitChanges();
        //    }
        //}   

        //private static void setSymmetryAttributes(Attribute attribute, string clusterName)
        //{            
        //    List<Attribute> symmetryAtts = new List<Attribute>();

        //    var syatts = (from symmetryAtt in db.AttributeSymmetryDBs
        //                  where symmetryAtt.attributeName == attribute.name
        //                  && symmetryAtt.clusterName == clusterName
        //                        //|symmetryAtt.symmetry == attribute.name
        //                  select symmetryAtt);

        //    if (syatts.Count() > 0)
        //    {
        //        foreach (var impatt in syatts)
        //        {
        //            if (attribute.name.Equals(impatt.symmetry) == false)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.symmetry, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                setPIIAndLiteralType(i_attribute, clusterName);
        //                setImpliedFromAttributes(i_attribute, clusterName);
        //                setImpliedToAttributes(i_attribute, clusterName);
        //                setSymmetryAttributes(i_attribute, clusterName);
        //                i_attribute.clusterName = clusterName;
        //                symmetryAtts.Add(i_attribute);
        //            }                    
        //        }
        //        attribute.setSymmetry(symmetryAtts);
        //    }
        //    setSymmetryAttributesRev(attribute, clusterName);
        //}

        //private static void setSymmetryAttributesRev(Attribute attribute, string clusterName)
        //{
        //    List<Attribute> symmetryAtts = new List<Attribute>();

        //    var syatts = (from symmetryAtt in db.AttributeSymmetryDBs
        //                  where symmetryAtt.symmetry == attribute.name
        //                  && symmetryAtt.clusterName == clusterName
        //                  select symmetryAtt);

        //    if (syatts.Count() > 0)
        //    {
        //        foreach (var impatt in syatts)
        //        {
        //            if (attribute.name.Equals(impatt.symmetry) == true)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.attributeName, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                //setPIIAndLiteralType(i_attribute);
        //                //setImpliedFromAttributes(i_attribute);
        //                //setImpliedToAttributes(i_attribute);
        //                //setSymmetryAttributes(i_attribute); 
        //                i_attribute.clusterName = clusterName;
        //                symmetryAtts.Add(i_attribute);
        //            }                   
        //        }
        //        attribute.setSymmetryInv(symmetryAtts);
        //    }
        //}

        //private static void setImpliedToAttributes(Attribute attribute, string clusterName)
        //{
        //    List<Attribute> impliedToAtts = new List<Attribute>();

        //    var itatts = (from impliedAtt in db.AttributeImpliedToDBs
        //                  where impliedAtt.attributeName == attribute.name
        //                  && impliedAtt.clusterName == clusterName
        //                        //|impliedAtt.impliedTo == attribute.name
        //                  select impliedAtt);

        //    if (itatts.Count() > 0)
        //    {
        //        foreach (var impatt in itatts)
        //        {
        //            if (attribute.name.Equals(impatt.impliedTo) == false)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.impliedTo, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                setPIIAndLiteralType(i_attribute, clusterName);
        //                setImpliedFromAttributes(i_attribute, clusterName);
        //                setImpliedToAttributes(i_attribute, clusterName);
        //                setSymmetryAttributes(i_attribute, clusterName);
        //                i_attribute.clusterName = clusterName;
        //                impliedToAtts.Add(i_attribute);
        //            }

        //        }
        //        attribute.setImpliedTo(impliedToAtts);
        //    }

        //    setImpliedToAttributesRev(attribute, clusterName);
        //}

        //private static void setImpliedToAttributesRev(Attribute attribute, string clusterName)
        //{
        //    List<Attribute> impliedToAtts = new List<Attribute>();

        //    var itatts = (from impliedAtt in db.AttributeImpliedToDBs
        //                  where impliedAtt.impliedTo == attribute.name
        //                  && impliedAtt.clusterName == clusterName
        //                  select impliedAtt);

        //    if (itatts.Count() > 0)
        //    {
        //        foreach (var impatt in itatts)
        //        {
        //            if (attribute.name.Equals(impatt.impliedTo) == true)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.attributeName, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                //setPIIAndLiteralType(i_attribute);
        //                //setImpliedFromAttributes(i_attribute);
        //                //setImpliedToAttributes(i_attribute);
        //                //setSymmetryAttributes(i_attribute);
        //                i_attribute.clusterName = clusterName;
        //                impliedToAtts.Add(i_attribute); 
        //            }

        //        }
        //        attribute.setImpliedToInv(impliedToAtts);
        //    }

        //}

        //private static void setImpliedFromAttributes(Attribute attribute, string clusterName)
        //{
        //    List<Attribute> impliedFromAtts = new List<Attribute>();

        //    var ifatts = (from impliedAtt in db.AttributeImpliedFromDBs
        //                  where impliedAtt.attributeName == attribute.name
        //                  && impliedAtt.clusterName == clusterName
        //                        //|impliedAtt.impliedFrom == attribute.name
        //                  select impliedAtt);

        //    if (ifatts.Count() > 0)
        //    {
        //        foreach (var impatt in ifatts)
        //        {
        //            if (attribute.name.Equals(impatt.impliedFrom) == false)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.impliedFrom, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                setPIIAndLiteralType(i_attribute, clusterName);
        //                setImpliedFromAttributes(i_attribute, clusterName);
        //                setImpliedToAttributes(i_attribute, clusterName);
        //                setSymmetryAttributes(i_attribute,clusterName);
        //                i_attribute.clusterName = clusterName;
        //                impliedFromAtts.Add(i_attribute);  
        //            }
        //        }

        //        attribute.setImpliedFrom(impliedFromAtts);
        //    }
        //    setImpliedFromAttributesRev(attribute, clusterName);

        //}

        //private static void setImpliedFromAttributesRev(Attribute attribute, string clusterName)
        //{
        //    List<Attribute> impliedFromAtts = new List<Attribute>();

        //    var ifatts = (from impliedAtt in db.AttributeImpliedFromDBs
        //                  where impliedAtt.impliedFrom == attribute.name
        //                  && impliedAtt.clusterName == clusterName
        //                  select impliedAtt);

        //    if (ifatts.Count() > 0)
        //    {
        //        foreach (var impatt in ifatts)
        //        {
        //            if (attribute.name.Equals(impatt.impliedFrom) == true)
        //            {
        //                Attribute i_attribute = findAttribute(impatt.attributeName, clusterName);
        //                //setPrivacyPreference(actorName, i_attribute);
        //                //setPIIAndLiteralType(i_attribute);
        //                //setImpliedFromAttributes(i_attribute);
        //                //setImpliedToAttributes(i_attribute);
        //                //setSymmetryAttributes(i_attribute);
        //                i_attribute.clusterName = clusterName;
        //                impliedFromAtts.Add(i_attribute);                                               
        //            }
        //        }

        //        attribute.setImpliedFromInv(impliedFromAtts);
        //    }
        //}

        private static List<string> allRelatedAggregationStatements;

        public static void loadAggregateAttributes(Attribute attribute, string clusterName)
        {
            //List<string> aggregateToAtts = findAttributeAggregateStatements(attribute.name);
            allRelatedAggregationStatements = new List<string>();
            List<string> aggregateToAtts = loadAllRelatedAggregationStatements(attribute, clusterName);
            attribute.setAggregationStatements(aggregateToAtts);
        }

        private static List<string> loadAllRelatedAggregationStatements(Attribute attribute, string clusterName)
        {
            List<string> aggregateToAtts = findAttributeAggregateStatements(attribute.name, clusterName);

            foreach (string agg_stat in aggregateToAtts)
            {
                bool contained = false;
                foreach (string s in allRelatedAggregationStatements)
                {
                    if (s.Equals(agg_stat))
                    {
                        contained = true;
                        break;
                    }
                }
                if (contained == false)
                {
                    allRelatedAggregationStatements.Add(agg_stat);
                    List<string> atts_in_related_stat = extractAttributesFromAggString(agg_stat, clusterName);
                    foreach (string att_r_s in atts_in_related_stat)
                    {
                        Attribute att_r = findAttribute(att_r_s, clusterName);
                        aggregateToAtts = findAttributeAggregateStatements(att_r.name, clusterName);

                        if (aggregateToAtts.Count > 0)
                        {
                            loadAllRelatedAggregationStatements(attribute, clusterName);
                        }
                    }
                }
            }
            return allRelatedAggregationStatements;
        }

        public static List<string> extractAttributesFromAggString(string att_agg, string clusterName)
        {
            List<Attribute> all_attributes = loadAllAttributes(clusterName);
            List<string> attributesInString = new List<string>();

            foreach (Attribute att in all_attributes)
            {
                if (att_agg.Contains(att.name))
                {
                    attributesInString.Add(att.name);
                }
            }

            return attributesInString;
        }


        public static Attribute findAttribute(string attributeName, string clusterName)
        {
            Attribute attribute = null;
            AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
            attribute = attDB.attribute;

            return attribute;

            //var att = (from attribute_ in db.AttributeDBs
            //           where attribute_.attributeName == attributeName
            //           && attribute_.clusterName == clusterName
            //           select attribute_).SingleOrDefault<AttributeDB>();

            //if (att == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att.attributeName;
            //    attribute.literalType = att.attributeLiteralType;
            //    attribute.category = att.category;
            //    attribute.description = att.description;
            //    attribute.clusterName = clusterName;
            //    if (att.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }
            //    return attribute;
            //}
        }

        //public static ActorDB findActorAttribute(String actorName, string attributeName, string clusterName)
        //{
        //    var act_att = (from act_att_ in db.ActorDBs
        //               where act_att_.actorName == actorName 
        //               && act_att_.actorAttribute == attributeName
        //               && act_att_.clusterName == clusterName
        //                   select act_att_).SingleOrDefault<ActorDB>();

        //    if (act_att == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return act_att;
        //    }
        //}

        //public static ActorRoleDB findActorRole(string actorName, string roleName, string clusterName)
        //{
        //    var act_att = (from act_att_ in db.ActorRoleDBs
        //               where act_att_.actorName == actorName 
        //               && act_att_.roleName == roleName
        //               && act_att_.clusterName == clusterName
        //                   select act_att_).SingleOrDefault<ActorRoleDB>();

        //    if (act_att == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return act_att;
        //    }
        //}        

        //public static AttributeImpliedFromDB findImpliedFromAttribute(String attributeName, string impliedFromAttribute, string clusterName)
        //{
        //    var imp_att = (from imp_att_ in db.AttributeImpliedFromDBs
        //                   where imp_att_.attributeName == attributeName 
        //                   && imp_att_.impliedFrom == impliedFromAttribute
        //                   && imp_att_.clusterName == clusterName
        //                   select imp_att_).SingleOrDefault<AttributeImpliedFromDB>();

        //    if (imp_att == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return imp_att;
        //    }
        //}

        //public static AttributeImpliedToDB findImpliedToAttribute(String attributeName, string impliedToAttribute, string clusterName)
        //{
        //    var imp_att = (from imp_att_ in db.AttributeImpliedToDBs
        //                   where imp_att_.attributeName == attributeName 
        //                   && imp_att_.impliedTo == impliedToAttribute
        //                   && imp_att_.clusterName == clusterName
        //                   select imp_att_).SingleOrDefault<AttributeImpliedToDB>();

        //    if (imp_att == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return imp_att;
        //    }
        //}

        //public static AttributeSymmetryDB findSymmetryAttribute(String attributeName, string symmetryAttribute, string clusterName)
        //{
        //    var sym_att = (from sym_att_ in db.AttributeSymmetryDBs
        //                   where sym_att_.attributeName == attributeName 
        //                   && sym_att_.symmetry == symmetryAttribute
        //                   && sym_att_.clusterName == clusterName
        //                   select sym_att_).SingleOrDefault<AttributeSymmetryDB>();

        //    if (sym_att == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return sym_att;
        //    }
        //}

        public static List<string> findAttributeAggregateStatements(string attributeName, string clusterName)
        {
            List<string> att_statements = new List<string>();
            List<string> attAggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string attAggName in attAggNames)
            {
                AttributeAggregationStatementDB aggs = AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, attAggName);
                if (aggs.aggregationStatement.Contains(attributeName))
                {
                    att_statements.Add(aggs.aggregationStatement);
                }
            }

            //List<string> att_statements = new List<string>();

            //var agg_from_att = (from att_agg_stat in db.AttributeAggregationStatementDBs
            //                    where att_agg_stat.clusterName == clusterName
            //                    select att_agg_stat);

            //foreach (AttributeAggregationStatementDB aas in agg_from_att)
            //{
            //    if (aas.aggregationStatement.Contains(attributeName))
            //    {
            //        att_statements.Add(aas.aggregationStatement);
            //    }
            //}
            return att_statements;
        }

        public static List<string> loadAllAggregationStatements(string clusterName)
        {
            List<string> att_statements = new List<string>();
            List<string> attAggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string attAggName in attAggNames)
            {
                AttributeAggregationStatementDB aggs = AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, attAggName);
                att_statements.Add(aggs.aggregationStatement);
            }

            //List<string> att_statements = new List<string>();

            //var agg_from_att = (from att_agg_stat in db.AttributeAggregationStatementDBs
            //                    where att_agg_stat.clusterName == clusterName
            //                    select att_agg_stat);

            //foreach (AttributeAggregationStatementDB aas in agg_from_att)
            //{
            //    att_statements.Add(aas.aggregationStatement);
            //}
            return att_statements;
        }

        ////public static List<string> loadAllPolicyStatements(string clusterName)
        ////{
        ////    List<string> p_statements = new List<string>();

        ////    var p_s = (from p_s_1 in db.PolicyStatementDBs
        ////               where p_s_1.clusterName == clusterName
        ////               select p_s_1);

        ////    foreach (PolicyStatementDB psx in p_s)
        ////    {
        ////        p_statements.Add(psx.policyStatement);
        ////    }
        ////    return p_statements;
        ////}

        public static List<Policy> loadAllPolicyStatementsFull(string clusterName)
        {
            List<Policy> p_statements = new List<Policy>();

            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementDB pol_s = PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyName);
                Policy policy = new Policy();
                policy.id = pol_s.policyID;
                policy.clusterName = clusterName;
                policy.statement = pol_s.policyStatement;
                policy.transmissionPrinciple = pol_s.transmissionPrinciple;
                p_statements.Add(policy);
            }

            //var p_s = (from p_s_1 in db.PolicyStatementDBs
            //           where p_s_1.clusterName == clusterName
            //           select p_s_1);

            //foreach (PolicyStatementDB psx in p_s)
            //{
            //    Policy policy = new Policy();
            //    policy.id = psx.policyID;
            //    policy.clusterName = clusterName;
            //    policy.statement = psx.policyStatement;
            //    policy.transmissionPrinciple = psx.transmissionPrinciple;
            //    p_statements.Add(policy);
            //}

            return p_statements;
        }

        public static Policy loadDoNothingPolicy(string clusterName)
        {
            Policy policy = null;
            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementDB pol_s = PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyName);
                if (pol_s.policyStatement.Equals("DO[NILL]"))
                {
                    policy = new Policy();
                    policy.id = pol_s.policyID;
                    policy.clusterName = clusterName;
                    policy.statement = pol_s.policyStatement;
                    policy.transmissionPrinciple = pol_s.transmissionPrinciple;
                }
            }

            //var p_s = (from p_s_1 in db.PolicyStatementDBs
            //           where p_s_1.clusterName == clusterName
            //           select p_s_1);

            //foreach (PolicyStatementDB psx in p_s)
            //{
            //    if (psx.policyStatement.Equals("DO[NILL]"))
            //    {
            //        policy = new Policy();
            //        policy.id = psx.policyID;
            //        policy.clusterName = clusterName;
            //        policy.statement = psx.policyStatement;
            //        policy.transmissionPrinciple = psx.transmissionPrinciple;
            //    }
            //}

            return policy;
        }

        private static bool aggregateStatementExist(string statement, string clusterName)
        {
            bool exist = false;
            List<string> attAggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string attAggName in attAggNames)
            {
                AttributeAggregationStatementDB aggs = AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, attAggName);
                if (aggs.aggregationStatement.Equals(statement))
                {
                    exist = true;
                    break;
                }
            }

            //var agg_from_att = (from att_agg_stat in db.AttributeAggregationStatementDBs
            //                    where att_agg_stat.clusterName == clusterName
            //                    select att_agg_stat);
            //foreach (AttributeAggregationStatementDB aas in agg_from_att)
            //{
            //    if (aas.aggregationStatement.Equals(statement))
            //    {
            //        exist = true;
            //        break;
            //    }
            //}

            return exist;
        }

        private static bool policyStatementExist(string statement, string transmissionPrinciple, string clusterName)
        {
            bool exist = false;
            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementDB pol_s = PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyName);
                if (pol_s.policyStatement.Equals(statement) && pol_s.transmissionPrinciple.Equals(transmissionPrinciple))
                {
                    exist = true;
                    break;
                }
            }

            //var p_s = (from p_stat in db.PolicyStatementDBs
            //           where p_stat.clusterName == clusterName
            //           && p_stat.transmissionPrinciple == transmissionPrinciple
            //           select p_stat);

            //foreach (PolicyStatementDB p in p_s)
            //{
            //    if (p.policyStatement.Equals(statement))
            //    {
            //        exist = true;
            //        break;
            //    }
            //}

            return exist;
        }


        private static bool attributeDomainConstraintExist(string att_name, string domain_con, string clusterName)
        {
            Attribute attribute = null;
            bool exist = false;
            AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, att_name);
            attribute = attDB.attribute;

            foreach (string d_constraint in attribute.domain)
            {
                if (d_constraint.Equals(domain_con))
                {
                    exist = true;
                    break;
                }
            }
            return exist;

            //var att_dom = (from att_dom_s in db.ConstraintDBs
            //               where att_dom_s.attributeName == att_name
            //                       && att_dom_s.attributeDomainConstraint == domain_con
            //                       && att_dom_s.clusterName == clusterName
            //               select att_dom_s).SingleOrDefault<ConstraintDB>(); ;

            //if (att_dom != null)
            //{
            //    return true;
            //}

            //return false;
        }
        public static bool attributeTypeOfExist(string child_att, string parent_att, string clusterName)
        {
            Attribute attribute = null;
            bool exist = false;

            AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, child_att);
            attribute = attDB.attribute;

            foreach (string type_of in attribute.type_of)
            {
                if (type_of.Equals(parent_att))
                {
                    exist = true;
                    break;
                }
            }
            return exist;

            //var att_to = (from att_to_s in db.AttributeTypeOfDBs
            //               where att_to_s.childAttributeName == child_att
            //                       && att_to_s.parentAttributeName == parent_att
            //                       && att_to_s.clusterName == clusterName
            //               select att_to_s).SingleOrDefault<AttributeTypeOfDB>(); ;

            //if (att_to != null)
            //{
            //    return true;
            //}

            //return false;
        }

        //private static void setPrivacyPreference(string actorName, Attribute attribute, string clusterName)
        //{
        //    var actor_ = (from actor in db.ActorDBs
        //                           where actor.actorName == actorName
        //                           && actor.actorAttribute == attribute.name
        //                           && actor.clusterName == clusterName
        //                           select actor).SingleOrDefault<ActorDB>();

        //    if (actor_ != null)
        //    {
        //        if (actor_.isPrivateAttribute.Trim().Equals("true"))
        //        {
        //            attribute.isPrivate = true;
        //        }
        //        else
        //        {
        //            attribute.isPrivate = false;
        //        }
        //    }
        //}

        //private static void setPIIAndLiteralType(Attribute attribute, string clusterName)
        //{
        //    var att_piilt = (from att_ in db.AttributeDBs
        //                           where att_.attributeName == attribute.name
        //                           && att_.clusterName == clusterName
        //                           select att_).SingleOrDefault<AttributeDB>();

        //    if (att_piilt != null)
        //    {
        //        if (att_piilt.personallyIdentifiable.Trim().Equals("true"))
        //        {
        //            attribute.personallyIdentifiable = true;
        //        }
        //        else
        //        {
        //            attribute.personallyIdentifiable = false;
        //        }
        //        attribute.literalType = att_piilt.attributeLiteralType;
        //    }

        //}

        //private static void setAttributeDomainValueConstraints(Attribute attribute, string clusterName)
        //{
        //    var att_dom = (from att_dom_s in db.ConstraintDBs
        //                   where att_dom_s.attributeName == attribute.name
        //                   && att_dom_s.clusterName == clusterName
        //                   select att_dom_s);

        //    foreach (ConstraintDB c_db in att_dom)
        //    {
        //        attribute.addDomain(c_db.attributeDomainConstraint);
        //    }

        //}

        //private static void setAttributeTypeOf(Attribute attribute, string clusterName)
        //{
        //    var att_to = (from att_to_s in db.AttributeTypeOfDBs
        //                  where att_to_s.childAttributeName == attribute.name
        //                  && att_to_s.clusterName ==  clusterName
        //                  select att_to_s);

        //    foreach (AttributeTypeOfDB c_db in att_to)
        //    {
        //        attribute.addTypeOf(c_db.parentAttributeName);
        //    }

        //}

        public static List<string> loadActorRoles(string actorName, string clusterName)
        {
            ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
            List<string> roles = actorDB.actor.roles;

            //var roles_ = (from rs in db.ActorRoleDBs
            //           where rs.actorName == actorName
            //           && rs.clusterName == clusterName
            //           select rs);

            //foreach (var rn in roles_)
            //{
            //    roles.Add(rn.roleName);
            //}

            return roles;
        }

        private static List<Attribute> loadAttributes(string actorName, string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
            Actor actor = actorDB.actor;

            return actor.attributes;

            //List<string> actorAttributeNames = loadAttributeNames(actorName, clusterName);

            //foreach (string att_n in actorAttributeNames)
            //{
            //    //processed_relations = new List<string>();

            //    var att = (from attribute_ in db.AttributeDBs
            //               where attribute_.attributeName == att_n
            //               && attribute_.clusterName == clusterName
            //               select attribute_).SingleOrDefault<AttributeDB>();

            //    if (att != null)
            //    {
            //        Attribute attribute = new Attribute();
            //        attribute.name = att.attributeName;
            //        attribute.clusterName = clusterName;
            //        attribute.literalType = att.attributeLiteralType;
            //        attribute.category = att.category;
            //        attribute.description = att.description;
            //        attribute.attributeValue = att.attributeValue;

            //        if (att.personallyIdentifiable.Trim().Equals("true"))
            //        {
            //            attribute.personallyIdentifiable = true;
            //        }
            //        else
            //        {
            //            attribute.personallyIdentifiable = false;
            //        }
            //        setPrivacyPreference(actorName, attribute, clusterName);

            //        setImpliedFromAttributes(attribute, clusterName);
            //        //if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //        setImpliedToAttributes(attribute, clusterName);
            //        //if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //        setSymmetryAttributes(attribute, clusterName);
            //        //if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //        setAttributeDomainValueConstraints(attribute, clusterName);
            //        setAttributeTypeOf(attribute, clusterName);

            //        attributes.Add(attribute);
            //    }                
            //}
            //return attributes;
        }
        public static Attribute loadAttributeFull(string att_n, string clusterName)
        {
            AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, att_n);
            if (attDB != null)
            {
                return attDB.attribute;
            }
            else
            {
                return null;
            }

            //var att = (from attribute_ in db.AttributeDBs
            //               where attribute_.attributeName == att_n
            //               && attribute_.clusterName == clusterName
            //               select attribute_).SingleOrDefault<AttributeDB>();

            //if (att != null)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att.attributeLiteralType;
            //    attribute.category = att.category;
            //    attribute.description = att.description;
            //    attribute.attributeValue = att.attributeValue;
            //    if (att.personallyIdentifiable.Trim().Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    setImpliedFromAttributes(attribute, clusterName);
            //    if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //    setImpliedToAttributes(attribute, clusterName);
            //    if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //    setSymmetryAttributes(attribute, clusterName);
            //    if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //    setAttributeDomainValueConstraints(attribute, clusterName);
            //    setAttributeTypeOf(attribute, clusterName);

            //    return attribute;
            //}

            //return null;
        }

        public static List<string> loadAllAttributeNames(string clusterName)
        {
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            return attributeNames;
        }

        public static List<Attribute> loadAllAttributes(string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            foreach (string attributeName in attributeNames)
            {
                AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
                attributes.Add(attDB.attribute);
            }

            return attributes;

            //List<Attribute> attributes = new List<Attribute>();
            //var atts = (from attribute_ in db.AttributeDBs
            //            where attribute_.clusterName == clusterName
            //           select attribute_);

            //foreach (var att_ in atts)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att_.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att_.attributeLiteralType;
            //    attribute.category = att_.category;
            //    attribute.description = att_.description;
            //    attribute.attributeValue = att_.attributeValue;
            //    if (att_.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    attributes.Add(attribute);
            //}
            //return attributes;
        }

        public static List<Attribute> loadAgentAttributes(string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            foreach (string attributeName in attributeNames)
            {
                AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
                if (attDB.attribute.category.Equals("agentAttribute"))
                {
                    attributes.Add(attDB.attribute);
                }
            }

            return attributes;

            //List<Attribute> attributes = new List<Attribute>();
            //var atts = (from attribute_ in db.AttributeDBs
            //            where attribute_.category == "agentAttribute"
            //            && attribute_.clusterName == clusterName
            //            select attribute_);

            //foreach (var att_ in atts)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att_.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att_.attributeLiteralType;
            //    attribute.category = att_.category;
            //    attribute.description = att_.description;
            //    attribute.attributeValue = att_.attributeValue;
            //    if (att_.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    setImpliedFromAttributes(attribute, clusterName);
            //    if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //    setImpliedToAttributes(attribute, clusterName);
            //    if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //    setSymmetryAttributes(attribute, clusterName);
            //    if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //    setAttributeDomainValueConstraints(attribute, clusterName);
            //    setAttributeTypeOf(attribute, clusterName);

            //    attributes.Add(attribute);
            //}
            //return attributes;
        }


        public static List<Attribute> loadRegulatoryAttributes(string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            foreach (string attributeName in attributeNames)
            {
                AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
                if (attDB.attribute.category.Equals("regulatoryAttribute"))
                {
                    attributes.Add(attDB.attribute);
                }
            }

            return attributes;

            //List<Attribute> attributes = new List<Attribute>();
            //var atts = (from attribute_ in db.AttributeDBs
            //            where attribute_.category == "regulatoryAttribute"
            //            && attribute_.clusterName == clusterName
            //            select attribute_);

            //foreach (var att_ in atts)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att_.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att_.attributeLiteralType;
            //    attribute.category = att_.category;
            //    attribute.description = att_.description;
            //    attribute.attributeValue = att_.attributeValue;
            //    if (att_.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    setImpliedFromAttributes(attribute, clusterName);
            //    if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //    setImpliedToAttributes(attribute, clusterName);
            //    if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //    setSymmetryAttributes(attribute, clusterName);
            //    if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //    setAttributeDomainValueConstraints(attribute, clusterName);
            //    setAttributeTypeOf(attribute, clusterName);

            //    attributes.Add(attribute);
            //}
            //return attributes;
        }

        public static List<Attribute> loadEnvironmentAttributes(string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            foreach (string attributeName in attributeNames)
            {
                AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
                if (attDB.attribute.category.Equals("environmentalAttribute"))
                {
                    attributes.Add(attDB.attribute);
                }
            }

            return attributes;

            //List<Attribute> attributes = new List<Attribute>();
            //var atts = (from attribute_ in db.AttributeDBs
            //            where attribute_.category == "environmentalAttribute"
            //            && attribute_.clusterName == clusterName
            //            select attribute_);

            //foreach (var att_ in atts)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att_.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att_.attributeLiteralType;
            //    attribute.category = att_.category;
            //    attribute.description = att_.description;
            //    attribute.attributeValue = att_.attributeValue;
            //    if (att_.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    setImpliedFromAttributes(attribute, clusterName);
            //    if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //    setImpliedToAttributes(attribute, clusterName);
            //    if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //    setSymmetryAttributes(attribute, clusterName);
            //    if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //    setAttributeDomainValueConstraints(attribute, clusterName);
            //    setAttributeTypeOf(attribute, clusterName);

            //    attributes.Add(attribute);
            //}
            //return attributes;
        }

        public static List<Attribute> loadFunctionalAttributes(string clusterName)
        {
            List<Attribute> attributes = new List<Attribute>();
            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);

            foreach (string attributeName in attributeNames)
            {
                AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
                if (attDB.attribute.category.Equals("functionalAttribute"))
                {
                    attributes.Add(attDB.attribute);
                }
            }

            //List<Attribute> attributes = new List<Attribute>();
            //var atts = (from attribute_ in db.AttributeDBs
            //            where attribute_.category == "functionalAttribute"
            //            && attribute_.clusterName == clusterName
            //            select attribute_);

            //foreach (var att_ in atts)
            //{
            //    Attribute attribute = new Attribute();
            //    attribute.name = att_.attributeName;
            //    attribute.clusterName = clusterName;
            //    attribute.literalType = att_.attributeLiteralType;
            //    attribute.category = att_.category;
            //    attribute.attributeValue = att_.attributeValue;
            //    attribute.description = att_.description;
            //    if (att_.personallyIdentifiable.Equals("true"))
            //    {
            //        attribute.personallyIdentifiable = true;
            //    }
            //    else
            //    {
            //        attribute.personallyIdentifiable = false;
            //    }

            //    setImpliedFromAttributes(attribute, clusterName);
            //    if (attribute.implied_from.Count == 0)
            //        setImpliedFromAttributesRev(attribute, clusterName);

            //    setImpliedToAttributes(attribute, clusterName);
            //    if (attribute.implied_to.Count == 0)
            //        setImpliedToAttributesRev(attribute, clusterName);

            //    setSymmetryAttributes(attribute, clusterName);
            //    if (attribute.symmetry.Count == 0)
            //        setSymmetryAttributesRev(attribute, clusterName);

            //    setAttributeDomainValueConstraints(attribute, clusterName);
            //    setAttributeTypeOf(attribute, clusterName);
            //    attributes.Add(attribute);
            //}

            //load callable states as attributes
            List<string> machines = loadAllmachines(clusterName);
            List<string> uniquestates = new List<string>();
            foreach (string machineName in machines)
            {
                List<State> states = loadAllStates(machineName, clusterName);
                foreach (State state in states)
                {
                    if (state.isCallable == true)
                    {
                        bool contained = false;
                        foreach (string s in uniquestates)
                        {
                            if (s.Equals(state.name))
                            {
                                contained = true;
                                break;
                            }
                        }
                        if (!contained)
                        {
                            uniquestates.Add(state.name);
                            Attribute att = new Attribute();
                            att.name = state.name;
                            att.clusterName = clusterName;
                            att.literalType = "[boolean]";
                            att.category = "functionalAttribute";
                            att.description = "";
                            att.personallyIdentifiable = false;

                            attributes.Add(att);
                        }

                    }
                }
            }

            return attributes;
        }

        private static List<string> loadAttributeNames(String actorName, string clusterName)
        {
            ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
            List<string> actor_attributes = new List<string>();

            foreach (Attribute att in actorDB.actor.attributes)
            {
                actor_attributes.Add(att.name);
            }

            //List<string> actor_attributes = new List<string>();

            //var act_atts_ =
            //    (from actor in db.ActorDBs
            //     where actor.actorName == actorName
            //     && actor.clusterName == clusterName
            //     select actor.actorAttribute);

            //foreach (string act_att in act_atts_)
            //{
            //    actor_attributes.Add(act_att);
            //}

            return actor_attributes;
        }

        public static void deleteActorAttribute(string attributeName, string clusterName)
        {
            List<string> actorNames = ActorSerialisationEngine.getActorsList(clusterName);
            foreach (string actorName in actorNames)
            {
                ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
                List<string> actor_attributes = new List<string>();

                foreach (Attribute att in actorDB.actor.attributes)
                {
                    if (att.name.Equals(attributeName))
                    {
                        ActorSerialisationEngine.deleteActorDB(actorName, clusterName);
                        actorDB.actor.attributes.Remove(att);
                        ActorSerialisationEngine.serialise(actorName, clusterName, actorDB);
                        break;
                    }
                }
            }

            //var actor_att_dbs =
            //(from actor in db.ActorDBs
            // where actor.actorAttribute == attributeName
            // && actor.clusterName == clusterName
            // select actor);
            //foreach (ActorDB actor_db in actor_att_dbs)
            //{
            //    db.ActorDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteActorAttribute(string clusterName)
        {
            List<string> actorNames = ActorSerialisationEngine.getActorsList(clusterName);
            foreach (string actorName in actorNames)
            {
                ActorSerialisationEngine.deleteActorDB(actorName, clusterName);
            }

            //var actor_att_dbs =
            //(from actor in db.ActorDBs
            // where actor.clusterName == clusterName
            // select actor);
            //foreach (ActorDB actor_db in actor_att_dbs)
            //{
            //    db.ActorDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }


        public static void deleteActorAttribute(string actorName, string attributeName, string clusterName)
        {
            ActorDB actorDB = ActorSerialisationEngine.deSerialiseActorDB(clusterName, actorName);
            List<string> actor_attributes = new List<string>();

            foreach (Attribute att in actorDB.actor.attributes)
            {
                if (att.name.Equals(attributeName))
                {
                    ActorSerialisationEngine.deleteActorDB(actorName, clusterName);
                    actorDB.actor.attributes.Remove(att);
                    ActorSerialisationEngine.serialise(actorName, clusterName, actorDB);
                    break;
                }
            }

            //var actor_att_dbs =
            //(from actor in db.ActorDBs
            // where actor.actorName == actorName && actor.actorAttribute == attributeName
            // && actor.clusterName == clusterName
            // select actor);
            //foreach (ActorDB actor_db in actor_att_dbs)
            //{
            //    db.ActorDBs.DeleteOnSubmit(actor_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteAggregationStatementWithAttribute(string clusterName)
        {
            List<string> attAggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string attAggName in attAggNames)
            {
                AttributeAggregationStatementSerialisationEngine.deleteAttributeAggregationDB(attAggName, clusterName);
            }

            //var att_agg_dbs =
            //(from agg_statement in db.AttributeAggregationStatementDBs
            // where agg_statement.clusterName == clusterName
            // select agg_statement);

            //foreach (AttributeAggregationStatementDB stat_db in att_agg_dbs)
            //{
            //    db.AttributeAggregationStatementDBs.DeleteOnSubmit(stat_db);
            //    db.SubmitChanges();
            //}

        }
        public static void deleteAggregationStatementWithAttribute(string attributeName, string clusterName)
        {
            List<string> attAggNames = AttributeAggregationStatementSerialisationEngine.getAggregationStatementsList(clusterName);
            foreach (string attAggName in attAggNames)
            {
                AttributeAggregationStatementDB attAggDB = AttributeAggregationStatementSerialisationEngine.deSerialiseAttAggStatementDB(clusterName, attAggName);
                if (attAggDB.aggregationStatement.Contains(attributeName))
                {
                    AttributeAggregationStatementSerialisationEngine.deleteAttributeAggregationDB(attAggName, clusterName);
                }
            }

            //var att_agg_dbs =
            //(from agg_statement in db.AttributeAggregationStatementDBs
            // where agg_statement.clusterName == clusterName
            // select agg_statement);

            //foreach (AttributeAggregationStatementDB stat_db in att_agg_dbs)
            //{
            //    if (stat_db.aggregationStatement.Contains(attributeName))
            //    {
            //        db.AttributeAggregationStatementDBs.DeleteOnSubmit(stat_db);
            //        db.SubmitChanges();
            //    }
            //}
        }

        //public static void deleteSymmetryWithAttribute(string attributeName, string clusterName)
        //{
        //    var att_sym_dbs =
        //    (from sym_ in db.AttributeSymmetryDBs
        //     where sym_.clusterName == clusterName 
        //     && (sym_.attributeName == attributeName || sym_.symmetry == attributeName)
        //     select sym_);

        //    foreach (AttributeSymmetryDB sym_rel_db in att_sym_dbs)
        //    {
        //        db.AttributeSymmetryDBs.DeleteOnSubmit(sym_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteSymmetryWithAttribute(string clusterName)
        //{
        //    var att_sym_dbs =
        //    (from sym_ in db.AttributeSymmetryDBs
        //     where sym_.clusterName == clusterName
        //     select sym_);

        //    foreach (AttributeSymmetryDB sym_rel_db in att_sym_dbs)
        //    {
        //        db.AttributeSymmetryDBs.DeleteOnSubmit(sym_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteSymmetryWithAttribute(string attributeName, string symmetry, string clusterName)
        //{
        //    var att_sym_dbs =
        //    (from sym_ in db.AttributeSymmetryDBs
        //     where sym_.clusterName == clusterName
        //     && ((sym_.attributeName == attributeName && sym_.symmetry == symmetry) ||
        //     (sym_.attributeName == symmetry && sym_.symmetry == attributeName))
        //     select sym_);

        //    foreach (AttributeSymmetryDB sym_rel_db in att_sym_dbs)
        //    {
        //        db.AttributeSymmetryDBs.DeleteOnSubmit(sym_rel_db);
        //        db.SubmitChanges();
        //    }
        //}


        //public static void deleteImpliedFromWithAttribute(string attributeName, string clusterName)
        //{
        //    var att_imp_from_dbs =
        //    (from imp_from in db.AttributeImpliedFromDBs
        //     where imp_from.clusterName == clusterName
        //     && (imp_from.attributeName == attributeName || imp_from.impliedFrom == attributeName)
        //     select imp_from);

        //    foreach (AttributeImpliedFromDB imp_rel_db in att_imp_from_dbs)
        //    {
        //        db.AttributeImpliedFromDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteImpliedFromWithAttribute(string clusterName)
        //{
        //    var att_imp_from_dbs =
        //    (from imp_from in db.AttributeImpliedFromDBs
        //     where imp_from.clusterName == clusterName
        //     select imp_from);

        //    foreach (AttributeImpliedFromDB imp_rel_db in att_imp_from_dbs)
        //    {
        //        db.AttributeImpliedFromDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteImpliedFromWithAttribute(string attributeName, string impliedFrom, string clusterName)
        //{
        //    var att_imp_from_dbs =
        //    (from imp_from in db.AttributeImpliedFromDBs
        //     where imp_from.clusterName == clusterName
        //     && imp_from.attributeName == attributeName && imp_from.impliedFrom == impliedFrom
        //     select imp_from);

        //    foreach (AttributeImpliedFromDB imp_rel_db in att_imp_from_dbs)
        //    {
        //        db.AttributeImpliedFromDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteImpliedToWithAttribute(string attributeName, string clusterName)
        //{
        //    var att_imp_to_dbs =
        //    (from imp_to in db.AttributeImpliedToDBs
        //     where imp_to.clusterName == clusterName
        //     && (imp_to.attributeName == attributeName || imp_to.impliedTo == attributeName)
        //     select imp_to);

        //    foreach (AttributeImpliedToDB imp_rel_db in att_imp_to_dbs)
        //    {
        //        db.AttributeImpliedToDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteImpliedToWithAttribute(string clusterName)
        //{
        //    var att_imp_to_dbs =
        //    (from imp_to in db.AttributeImpliedToDBs
        //     where imp_to.clusterName == clusterName
        //     select imp_to);

        //    foreach (AttributeImpliedToDB imp_rel_db in att_imp_to_dbs)
        //    {
        //        db.AttributeImpliedToDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteImpliedToWithAttribute(string attributeName, string impliedTo, string clusterName)
        //{
        //    var att_imp_to_dbs =
        //    (from imp_to in db.AttributeImpliedToDBs
        //     where imp_to.clusterName == clusterName
        //     && imp_to.attributeName == attributeName && imp_to.impliedTo == impliedTo
        //     select imp_to);

        //    foreach (AttributeImpliedToDB imp_rel_db in att_imp_to_dbs)
        //    {
        //        db.AttributeImpliedToDBs.DeleteOnSubmit(imp_rel_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deletePolicyStatementWithAttribute(string attributeName, string clusterName)
        {
            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementDB policyDB = PolicyStatementSerialisationEngine.deSerialisePolicyStatementDB(clusterName, policyName);
                if (policyDB.policyStatement.Contains(attributeName))
                {
                    PolicyStatementSerialisationEngine.deletePolicyStatementDB(policyName, clusterName);
                }
            }

            //var att_policy_dbs =
            //(from policy_statement in db.PolicyStatementDBs
            // where  policy_statement.clusterName == clusterName 
            // select policy_statement);

            //foreach (PolicyStatementDB stat_db in att_policy_dbs)
            //{
            //    if (stat_db.policyStatement.Contains(attributeName))
            //    {
            //        db.PolicyStatementDBs.DeleteOnSubmit(stat_db);
            //        db.SubmitChanges();
            //    }
            //}
        }

        public static void deletePolicyStatementWithAttribute(string clusterName)
        {
            List<string> policyNames = PolicyStatementSerialisationEngine.getPolicyStatementsList(clusterName);
            foreach (string policyName in policyNames)
            {
                PolicyStatementSerialisationEngine.deletePolicyStatementDB(policyName, clusterName);
            }

            //var att_policy_dbs =
            //(from policy_statement in db.PolicyStatementDBs
            // where policy_statement.clusterName == clusterName
            // select policy_statement);

            //foreach (PolicyStatementDB stat_db in att_policy_dbs)
            //{
            //    db.PolicyStatementDBs.DeleteOnSubmit(stat_db);
            //    db.SubmitChanges();
            //}
        }

        //public static void deleteConstraintAttribute(string clusterName)
        //{
        //    var constraint_att_dbs =
        //    (from constraint in db.ConstraintDBs
        //     where constraint.clusterName == clusterName
        //     select constraint);
        //    foreach (ConstraintDB constraint_db in constraint_att_dbs)
        //    {
        //        db.ConstraintDBs.DeleteOnSubmit(constraint_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteConstraintAttribute(string attributeName, string clusterName)
        //{
        //    var constraint_att_dbs =
        //    (from constraint in db.ConstraintDBs
        //     where constraint.clusterName == clusterName
        //     && constraint.attributeName == attributeName
        //     select constraint);
        //    foreach (ConstraintDB constraint_db in constraint_att_dbs)
        //    {
        //        db.ConstraintDBs.DeleteOnSubmit(constraint_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteTypeOfAttribute(string attributeName, string clusterName)
        //{
        //    var type_of_att_dbs =
        //    (from type_of in db.AttributeTypeOfDBs
        //     where (type_of.clusterName == clusterName)
        //     && (type_of.childAttributeName == attributeName || type_of.parentAttributeName == attributeName)
        //     select type_of);
        //    foreach (AttributeTypeOfDB type_of_db in type_of_att_dbs)
        //    {
        //        db.AttributeTypeOfDBs.DeleteOnSubmit(type_of_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteTypeOfAttribute(string clusterName)
        //{
        //    var type_of_att_dbs =
        //    (from type_of in db.AttributeTypeOfDBs
        //     where (type_of.clusterName == clusterName)
        //     select type_of);
        //    foreach (AttributeTypeOfDB type_of_db in type_of_att_dbs)
        //    {
        //        db.AttributeTypeOfDBs.DeleteOnSubmit(type_of_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteTypeOfAttribute(string child_type, string parent_type, string clusterName)
        //{
        //    var type_of_att_dbs =
        //    (from type_of in db.AttributeTypeOfDBs
        //     where (type_of.clusterName == clusterName)
        //     && (type_of.childAttributeName == child_type || type_of.parentAttributeName == parent_type)
        //     select type_of);
        //    foreach (AttributeTypeOfDB type_of_db in type_of_att_dbs)
        //    {
        //        db.AttributeTypeOfDBs.DeleteOnSubmit(type_of_db);
        //        db.SubmitChanges();
        //    }
        //}


        public static void deleteConstraintAttribute(string attributeName, string attributeDomainConstraint, string clusterName)
        {
            AttributeDB attDB = AttributeSerialisationEngine.deSerialiseAttributeDB(clusterName, attributeName);
            AttributeSerialisationEngine.deleteAttributeDB(attributeName, clusterName);

            foreach (string domainAtt in attDB.attribute.domain)
            {
                if (domainAtt.Equals(attributeDomainConstraint))
                {
                    attDB.attribute.domain.Remove(domainAtt);
                    break;
                }
            }
            AttributeSerialisationEngine.serialise(attributeName, clusterName, attDB);

            //var constraint_att_dbs =
            //(from constraint in db.ConstraintDBs
            // where (constraint.clusterName == clusterName)
            // && (constraint.attributeName == attributeName
            // && constraint.attributeDomainConstraint == attributeDomainConstraint)
            // select constraint);
            //foreach (ConstraintDB constraint_db in constraint_att_dbs)
            //{
            //    db.ConstraintDBs.DeleteOnSubmit(constraint_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteAttribute(Attribute attribute, string clusterName)
        {
            deleteActorAttribute(attribute.name, clusterName);
            deleteAggregationStatementWithAttribute(attribute.name, clusterName);
            //deleteImpliedFromWithAttribute(attribute.name, clusterName);
            //deleteImpliedToWithAttribute(attribute.name, clusterName);
            //deleteSymmetryWithAttribute(attribute.name, clusterName);
            deletePolicyStatementWithAttribute(attribute.name, clusterName);
            //deleteConstraintAttribute(attribute.name, clusterName);
            //deleteTypeOfAttribute(attribute.name, clusterName);
            //deleteStateActionOperationalAttributeDBatt(attribute.name, clusterName);
            //deleteEventOperationalAttributeDBatt(attribute.name, clusterName);

            AttributeSerialisationEngine.deleteAttributeDB(attribute.name, clusterName);

            //var att_dbs =
            //    (from att_db in db.AttributeDBs
            //     where att_db.attributeName == attribute.name
            //     && att_db.clusterName == clusterName
            //     select att_db);

            //foreach (AttributeDB att_db in att_dbs)
            //{
            //    db.AttributeDBs.DeleteOnSubmit(att_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteAttribute(string clusterName)
        {
            deleteActorAttribute(clusterName);
            deleteAggregationStatementWithAttribute(clusterName);
            //deleteImpliedFromWithAttribute(clusterName);
            //deleteImpliedToWithAttribute(clusterName);
            //deleteSymmetryWithAttribute(clusterName);
            deletePolicyStatementWithAttribute(clusterName);
            //deleteConstraintAttribute(clusterName);
            //deleteTypeOfAttribute(clusterName);
            //deleteStateActionOperationalAttributeDBatt(clusterName);
            //deleteEventOperationalAttributeDB(clusterName);

            List<string> attributeNames = AttributeSerialisationEngine.getAttributesList(clusterName);
            foreach (string attributeName in attributeNames)
            {
                AttributeSerialisationEngine.deleteAttributeDB(attributeName, clusterName);
            }

            //var att_dbs =
            //    (from att_db in db.AttributeDBs
            //     where att_db.clusterName == clusterName
            //     select att_db);

            //foreach (AttributeDB att_db in att_dbs)
            //{
            //    db.AttributeDBs.DeleteOnSubmit(att_db);
            //    db.SubmitChanges();
            //}
        }

        public static Event findEvent(string eventName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            Event eve_ = null;
            foreach (Event event_ in smDB.stateMachine.events)
            {
                if (event_.name.Equals(eventName))
                {
                    eve_ = event_;
                    break;
                }
            }
            return eve_;

            //var eve = (from event_ in db.EventDBs
            //           where event_.clusterName == clusterName && event_.eventName == eventName && event_.machineName == machineName
            //           select event_).SingleOrDefault<EventDB>();

            //if (eve == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    Event event_ = new Event();
            //    event_.name = eve.eventName;
            //    event_.clusterName = clusterName;
            //    event_.description = eve.eventDescription;
            //    event_.machineName = machineName;
            //    event_.type = eve.eventType;
            //    if (eve.isMitigationPoint != null && eve.isMitigationPoint.Equals("true"))
            //    {
            //        event_.isMitigationPoint = true;
            //    }
            //    if (eve.isMisusePoint != null && eve.isMisusePoint.Equals("true"))
            //    {
            //        event_.isMisusePoint = true;
            //    }


            //    return event_;
            //}
        }

        public static StateMachineDB findStateMachine(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);

            return smDB;

            //var mcs = (from mc_ in db.StateMachineDBs
            //           where mc_.clusterName == clusterName
            //           && mc_.machineName == machineName

            //           select mc_).SingleOrDefault<StateMachineDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
        }

        public static DomainClusterDB findCluster(string clusterName)
        {
            List<string> dcs = DomainClusterSerialisationEngine.getDomainClusterList();
            DomainClusterDB dcDB = null;
            foreach (string dc in dcs)
            {
                if (dc.Equals(clusterName))
                {
                    dcDB = DomainClusterSerialisationEngine.deSerialiseDomainClusterDB(clusterName);
                    break;
                }
            }

            return dcDB;

            //var mcs = (from mc_ in db.DomainClusterDBs
            //           where mc_.clusterName == clusterName

            //           select mc_).SingleOrDefault<DomainClusterDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
        }

        public static GroupFunctionDB findGroupFunction(string groupFunctionName, string clusterName)
        {
            GroupFunctionDB gfDB = GroupFunctionSerialisationEngine.deSerialiseActorDB(clusterName, groupFunctionName);
            return gfDB;

            //var mcs = (from mc_ in db.GroupFunctionDBs
            //           where mc_.clusterName == clusterName
            //           && mc_.functionName == groupFunctionName
            //           select mc_).SingleOrDefault<GroupFunctionDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
        }

        public static PolicyFunctionDB findPolicyFunction(string policyFunctionName, string clusterName)
        {
            PolicyFunctionDB pfDB = PolicyFunctionSerialisationEngine.deSerialiseActorDB(clusterName, policyFunctionName);
            return pfDB;

            //var mcs = (from mc_ in db.PolicyFunctionDBs
            //           where mc_.clusterName == clusterName
            //           && mc_.functionName == policyFunctionName
            //           select mc_).SingleOrDefault<PolicyFunctionDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
        }

        //public static StateMachineStartStateDB findStateMachineStartState(string machineName, string clusterName)
        //{

        //    var mcs = (from mc_ in db.StateMachineStartStateDBs
        //               where mc_.clusterName == clusterName
        //               && mc_.machineName == machineName

        //               select mc_).SingleOrDefault<StateMachineStartStateDB>();

        //    if (mcs == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return mcs;
        //    }
        //}

        //public static StateMachineEndStateDB findStateMachineEndState(string machineName, string clusterName)
        //{
        //    var mcs = (from mc_ in db.StateMachineEndStateDBs
        //               where mc_.clusterName == clusterName
        //               && mc_.machineName == machineName

        //               select mc_).SingleOrDefault<StateMachineEndStateDB>();

        //    if (mcs == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return mcs;
        //    }
        //}


        //public static StateDB findState(string machineName, string stateName, string clusterName)
        //{
        //    var mcs = (from mc_ in db.StateDBs
        //               where mc_.clusterName == clusterName
        //               && mc_.machineName == machineName && mc_.stateName == stateName
        //               select mc_).SingleOrDefault<StateDB>();

        //    if (mcs == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return mcs;
        //    }
        //}

        public static StateActionMitigationReferencesDB findStateActionMitigationReferenceAction(StateActionMitigationReferencesDB s_db)
        {
            //var mcs = (from mc_ in db.StateActionMitigationReferencesDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.actionName == s_db.actionName
            //           && mc_.referencedActionID == s_db.referencedActionID
            //           select mc_).SingleOrDefault<StateActionMitigationReferencesDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static StateActionMitigationReferencesDB findStateActionMitigationReferenceState(StateActionMitigationReferencesDB s_db)
        {
            //var mcs = (from mc_ in db.StateActionMitigationReferencesDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.actionName == s_db.actionName
            //           && mc_.referencedStateID == s_db.referencedStateID
            //           select mc_).SingleOrDefault<StateActionMitigationReferencesDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //return null;
            //}
            return null;
        }

        public static StateActionMitigationReferencesDB findStateActionMitigationReferenceTransition(StateActionMitigationReferencesDB s_db)
        {
            //var mcs = (from mc_ in db.StateActionMitigationReferencesDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.actionName == s_db.actionName
            //           && mc_.referencedTransitionID == s_db.referencedTransitionID
            //           select mc_).SingleOrDefault<StateActionMitigationReferencesDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static StateMitigationReferenceDB findStateMitigationReferenceAction(StateMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.StateMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.referencedActionID == s_db.referencedActionID
            //           select mc_).SingleOrDefault<StateMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static StateMitigationReferenceDB findStateMitigationReferenceState(StateMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.StateMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.referencedStateID == s_db.referencedStateID
            //           select mc_).SingleOrDefault<StateMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static StateMitigationReferenceDB findStateMitigationReferenceTransition(StateMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.StateMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.stateName == s_db.stateName
            //           && mc_.referencedTransitionID == s_db.referencedTransitionID
            //           select mc_).SingleOrDefault<StateMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static TransitionMitigationReferenceDB findTransitionMitigationReferenceAction(TransitionMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.TransitionMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.transitionName == s_db.transitionName
            //           && mc_.referencedActionID == s_db.referencedActionID
            //           select mc_).SingleOrDefault<TransitionMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static TransitionMitigationReferenceDB findTransitionMitigationReferenceState(TransitionMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.TransitionMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.transitionName == s_db.transitionName
            //           && mc_.referencedStateID == s_db.referencedStateID
            //           select mc_).SingleOrDefault<TransitionMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        public static TransitionMitigationReferenceDB findTransitionMitigationReferenceTransition(TransitionMitigationReferenceDB s_db)
        {
            //var mcs = (from mc_ in db.TransitionMitigationReferenceDBs
            //           where mc_.clusterName == s_db.clusterName
            //           && mc_.machineName == s_db.machineName
            //           && mc_.transitionName == s_db.transitionName
            //           && mc_.referencedTransitionID == s_db.referencedTransitionID
            //           select mc_).SingleOrDefault<TransitionMitigationReferenceDB>();

            //if (mcs == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return mcs;
            //}
            return null;
        }

        //public static StateTransitionDB findStateTransition(Transition transition, int policyID, string clusterName)
        //{
        //    var mcs = (from mc_ in db.StateTransitionDBs
        //               where mc_.clusterName == clusterName
        //               && mc_.machineName == transition.machineName
        //               && mc_.transitionName == transition.transitionName
        //               && mc_.associatedEvent == transition.associatedEevent
        //               && mc_.startState == transition.startState
        //               && mc_.endState == transition.endState
        //               && mc_.policyID == policyID
        //               select mc_).SingleOrDefault<StateTransitionDB>();

        //    if (mcs == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return mcs;
        //    }
        //}

        //public static EventTriggerDB findEventTrigger(string eventName, string trigger, string machineName, string clusterName)
        //{
        //    var eve_oa = (from event_oa_ in db.EventTriggerDBs
        //               where event_oa_.clusterName == clusterName
        //               && event_oa_.eventName == eventName 
        //               && event_oa_.eventTrigger == trigger
        //               && event_oa_.machineName == machineName
        //                  select event_oa_).SingleOrDefault<EventTriggerDB>();

        //    if (eve_oa == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return eve_oa;
        //    }
        //}

        //public static StateActionDB findStateAction(string machineName, string stateName, string actionName, string clusterName)
        //{
        //    var sta_oa = (from sta_oa_ in db.StateActionDBs
        //                  where sta_oa_.clusterName == clusterName
        //                  && sta_oa_.machineName == machineName
        //                  && sta_oa_.stateName == stateName
        //                  && sta_oa_.name == actionName
        //                  select sta_oa_).SingleOrDefault<StateActionDB>();

        //    if (sta_oa == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return sta_oa;
        //    }
        //}

        //public static StateActionOperationalAttributeDB findStateActionOperationalAttribute(string actionName,string stateName,string machineName, string attributeName, string clusterName)
        //{
        //    var sta_oa = (from sta_oa_ in db.StateActionOperationalAttributeDBs
        //                  where sta_oa_.clusterName == clusterName
        //                  && sta_oa_.machineName == machineName
        //                  && sta_oa_.stateName == stateName
        //                  && sta_oa_.actionName== actionName
        //                  && sta_oa_.operationalAttribute == attributeName
        //                  select sta_oa_).SingleOrDefault<StateActionOperationalAttributeDB>();

        //    if (sta_oa == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return sta_oa;
        //    }
        //}

        //public static EventOperationalAttributeDB findEventOperationalAttribute(string eventName, string machineName, string attributeName, string clusterName)
        //{
        //    var sta_oa = (from sta_oa_ in db.EventOperationalAttributeDBs
        //                  where sta_oa_.clusterName == clusterName
        //                  && sta_oa_.machineName == machineName
        //                  && sta_oa_.eventName == eventName
        //                  && sta_oa_.operationalAttribute == attributeName
        //                  select sta_oa_).SingleOrDefault<EventOperationalAttributeDB>();

        //    if (sta_oa == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return sta_oa;
        //    }
        //}

        public static void addEventDB(Event event_, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, event_.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(event_.machineName, clusterName);
            smDB.stateMachine.addEvent(event_);
            StateMachineSerialisationEngine.serialise(event_.machineName, clusterName, smDB);


            //addStateMachineDB(event_.machineName, clusterName);

            //EventDB event_db = new EventDB();
            //event_db.eventName = event_.name;
            //event_db.eventDescription = event_.description;
            //event_db.machineName = event_.machineName;
            //event_db.eventType = event_.type;
            //event_db.clusterName = clusterName;

            //Event event_exist = findEvent(event_.name, event_.machineName, clusterName);
            //if (event_exist == null)
            //{
            //    db.EventDBs.InsertOnSubmit(event_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateEventDB(event_, clusterName);
            //}

            //foreach (string trigger in event_.triggers)
            //{
            //    addEventTriggerDB(event_, trigger, clusterName);
            //}

            ////foreach (string operationalAttribute in event_.operationalAttributes_)
            ////{
            ////    addEventOperationalAttributeDB(event_, operationalAttribute, clusterName);
            ////}

            //filterEventTriggerDB(event_, clusterName);
            //filterEventOperationalAttributeDB(event_, clusterName);

        }

        //private static void addStateMachineDB(string stateMachine, string clusterName)
        public static void addStateMachineDB(StateMachine stateMachine, string clusterName)
        {
            List<string> smNames = StateMachineSerialisationEngine.getStateMachineList(clusterName);
            bool exist = false;
            foreach (string smName in smNames)
            {
                if (smName.Equals(stateMachine.machineName))
                {
                    exist = true;
                    StateMachineSerialisationEngine.deleteStateMachineDB(stateMachine.machineName, clusterName);
                    StateMachineDB smDB = new StateMachineDB(stateMachine);
                    StateMachineSerialisationEngine.serialise(stateMachine.machineName, clusterName, smDB);

                    break;
                }
            }
            if (exist == false)
            {
                StateMachineDB smDB = new StateMachineDB(stateMachine);
                StateMachineSerialisationEngine.serialise(stateMachine.machineName, clusterName, smDB);
            }

            //StateMachineDB sm_db = new StateMachineDB();
            //sm_db.machineName = stateMachine;
            //sm_db.clusterName = clusterName;

            //StateMachineDB machine_exist = findStateMachine(stateMachine, clusterName);
            //if (machine_exist == null)
            //{
            //    db.StateMachineDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
        }

        public static void cloneStateMachineDB(string originalmachineName, string cloneMachineName, string clusterName)
        {
            List<string> smNames = StateMachineSerialisationEngine.getStateMachineList(clusterName);
            foreach (string smName in smNames)
            {
                if (smName.Equals(cloneMachineName))
                {
                    return; //sm name already exist
                }
            }
            StateMachineDB smDBClone = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, originalmachineName);
            smDBClone.stateMachine.machineName = cloneMachineName;
            smDBClone.stateMachine.clusterName = clusterName;
            foreach (Event ev in smDBClone.stateMachine.events)
            {
                ev.machineName = cloneMachineName;
            }

            foreach (State state in smDBClone.stateMachine.states)
            {
                state.machineName = cloneMachineName;
                foreach (PMisuseAn.behaviour.model.Action action in state.actions)
                {
                    action.machineName = cloneMachineName;
                }
            }
            foreach (Transition trans in smDBClone.stateMachine.transitions)
            {
                trans.machineName = cloneMachineName;
            }
            if (smDBClone.stateMachine.startState != null)
            {
                smDBClone.stateMachine.startState.machineName = cloneMachineName;
            }
            if (smDBClone.stateMachine.endState != null)
            {
                smDBClone.stateMachine.endState.machineName = cloneMachineName;
            }

            StateMachineSerialisationEngine.serialise(cloneMachineName, clusterName, smDBClone);


            //StateMachine originalMachine = loadStateMachineDB(originalmachineName, clusterName);
            //StateMachine cloneMachine = new StateMachine();

            //cloneMachine.machineName = cloneMachineName;
            //cloneMachine.clusterName = originalMachine.clusterName;

            //foreach (Event event_ in originalMachine.events)
            //{
            //    event_.machineName = cloneMachineName;
            //    cloneMachine.addEvent(event_);
            //}

            //foreach (State state in originalMachine.states)
            //{
            //    state.machineName = cloneMachineName;
            //    List<PMisuseAn.behaviour.model.Action> cloneActions = new List<PMisuseAn.behaviour.model.Action>();
            //    foreach (PMisuseAn.behaviour.model.Action action in state.actions)
            //    {
            //        PMisuseAn.behaviour.model.Action newAction = MisUseAnObjectsDB.loadStateActionDB(action.name, state.name, originalmachineName, MainWindow.clusterName);

            //        newAction.machineName = cloneMachineName;
            //        cloneActions.Add(newAction);
            //    }
            //    state.actions = cloneActions;
            //    cloneMachine.addState(state);
            //}

            //foreach (Transition trans in originalMachine.transitions)
            //{
            //    trans.machineName = cloneMachineName;
            //    cloneMachine.addStateTransition(trans);
            //}

            //StateMachineDB sm_db = new StateMachineDB();
            //sm_db.machineName = cloneMachine.machineName;
            //sm_db.clusterName = clusterName;

            //StateMachineDB machine_exist = findStateMachine(cloneMachine.machineName, clusterName);
            //if (machine_exist == null)
            //{
            //    db.StateMachineDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
            //foreach (Event event_ in cloneMachine.events)
            //{
            //    addEventDB(event_, clusterName);
            //    foreach (string oatt_s in event_.operationalAttributes_)
            //    {
            //        addEventOperationalAttributeDB(event_, oatt_s, clusterName);
            //    }
            //    foreach (string trig_s in event_.triggers)
            //    {
            //        addEventTriggerDB(event_, trig_s, clusterName);
            //    }

            //    filterEventTriggerDB(event_, clusterName);
            //    filterEventOperationalAttributeDB(event_, clusterName);
            //}

            //foreach (State state in cloneMachine.states)
            //{
            //    addStateDB(state, clusterName);
            //    foreach (PMisuseAn.behaviour.model.Action action in state.actions)
            //    {
            //        addStateActionDB(action, clusterName);

            //        foreach (string oatt_s in action.operationalAttributes_)
            //        {
            //            addStateActionOperationalAttributeDB(action, oatt_s, clusterName);
            //        }
            //        filterStateActionOperationalAttributeDB(action, clusterName);
            //    }
            //}

            //foreach (Transition trans in cloneMachine.transitions)
            //{
            //    addStateTransitionDB(trans, clusterName);
            //}

            ////clone end state, start state, and mitigation refrences
            //string originalStartState = getStartState(originalmachineName, MainWindow.clusterName);
            //string originalEndState = getEndState(originalmachineName, MainWindow.clusterName);
            //if (originalStartState != null)
            //{
            //    addStateMachineStartStateDB(originalStartState, cloneMachineName, clusterName);
            //}
            //if (originalEndState != null)
            //{
            //    addStateMachineEndStateDB(originalEndState, cloneMachineName, clusterName);
            //}

            //foreach (State state in originalMachine.states)
            //{
            //    MitigationReferences originalStateMitigationRefs = MisUseAnObjectsDB.loadStateMitigationReferencesDB(state.name, originalmachineName, MainWindow.clusterName);
            //    MitigationReferences cloneStateMitigationRefs = new MitigationReferences();

            //    foreach (State s in originalStateMitigationRefs.states)
            //    {
            //        s.machineName = cloneMachineName;
            //        cloneStateMitigationRefs.addState(s);
            //    }
            //    foreach (PMisuseAn.behaviour.model.Action a in originalStateMitigationRefs.actions)
            //    {
            //        a.machineName = cloneMachineName;
            //        cloneStateMitigationRefs.addAction(a);
            //    }
            //    foreach (Transition t in originalStateMitigationRefs.transitions)
            //    {
            //        t.machineName = cloneMachineName;
            //        cloneStateMitigationRefs.addTransition(t);
            //    }

            //    addStateMitigationReferencesDB(state.name, cloneStateMitigationRefs, cloneMachineName, clusterName);

            //    foreach (PMisuseAn.behaviour.model.Action action in state.actions)
            //    {
            //        MitigationReferences originalStateActionMitigationRefs = MisUseAnObjectsDB.loadStateActionMitigationReferencesDB(action.name, state.name, originalmachineName, MainWindow.clusterName);
            //        MitigationReferences cloneStateActionMitigationRefs = new MitigationReferences();

            //        foreach (State s in originalStateActionMitigationRefs.states)
            //        {
            //            s.machineName = cloneMachineName;
            //            cloneStateActionMitigationRefs.addState(s);
            //        }
            //        foreach (PMisuseAn.behaviour.model.Action a in originalStateActionMitigationRefs.actions)
            //        {
            //            a.machineName = cloneMachineName;
            //            cloneStateActionMitigationRefs.addAction(a);
            //        }
            //        foreach (Transition t in originalStateActionMitigationRefs.transitions)
            //        {
            //            t.machineName = cloneMachineName;
            //            cloneStateActionMitigationRefs.addTransition(t);
            //        }

            //        addStateActionMitigationReferencesDB(action.name, state.name, cloneStateActionMitigationRefs, cloneMachineName, clusterName);

            //    }
            //}

            //foreach (Transition trans in originalMachine.transitions)
            //{
            //    MitigationReferences originalTransitionMitigationRefs = MisUseAnObjectsDB.loadTransitionMitigationReferencesDB(trans.transitionName, originalmachineName, MainWindow.clusterName);
            //    MitigationReferences cloneTransitionMitigationRefs = new MitigationReferences();

            //    foreach (State s in originalTransitionMitigationRefs.states)
            //    {
            //        s.machineName = cloneMachineName;
            //        cloneTransitionMitigationRefs.addState(s);
            //    }
            //    foreach (PMisuseAn.behaviour.model.Action a in originalTransitionMitigationRefs.actions)
            //    {
            //        a.machineName = cloneMachineName;
            //        cloneTransitionMitigationRefs.addAction(a);
            //    }
            //    foreach (Transition t in originalTransitionMitigationRefs.transitions)
            //    {
            //        t.machineName = cloneMachineName;
            //        cloneTransitionMitigationRefs.addTransition(t);
            //    }

            //    addTransitionMitigationReferencesDB(trans.transitionName, cloneTransitionMitigationRefs, cloneMachineName, clusterName);

            //}
        }

        public static void addClusterDB(string clusterName)
        {
            List<string> dClusters = DomainClusterSerialisationEngine.getDomainClusterList();
            bool exist = false;
            foreach (string dCluster in dClusters)
            {
                if (dCluster.Equals(clusterName))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == true)
            {
                return;
            }
            DomainClusterDB sm_db = new DomainClusterDB(clusterName);
            DomainClusterSerialisationEngine.serialise(sm_db, clusterName);

            //DomainClusterDB sm_db = new DomainClusterDB();
            //sm_db.clusterName = clusterName;

            //DomainClusterDB cluster_exist = findCluster(clusterName);
            //if (cluster_exist == null)
            //{
            //    db.DomainClusterDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addGroupFunctionDB(string groupFunctionName, string clusterName)
        {
            List<string> gfunctions = GroupFunctionSerialisationEngine.getGroupFunctionList(clusterName);
            bool exist = false;
            foreach (string gf in gfunctions)
            {
                if (gf.Equals(groupFunctionName))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == true)
            {
                return; //function name exist
            }
            GroupFunctionDB sm_db = new GroupFunctionDB(groupFunctionName);
            GroupFunctionSerialisationEngine.serialise(groupFunctionName, clusterName, sm_db);


            //GroupFunctionDB sm_db = new GroupFunctionDB();
            //sm_db.clusterName = clusterName;
            //sm_db.functionName = groupFunctionName;

            //GroupFunctionDB function_exist = findGroupFunction(groupFunctionName, clusterName);
            //if (function_exist == null)
            //{
            //    db.GroupFunctionDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addPolicyFunctionDB(string policyFunctionName, string clusterName)
        {
            List<string> pfunctions = PolicyFunctionSerialisationEngine.getPolicyFunctionList(clusterName);
            bool exist = false;
            foreach (string pf in pfunctions)
            {
                if (pf.Equals(policyFunctionName))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == true)
            {
                return; //function name exist
            }
            PolicyFunctionDB sm_db = new PolicyFunctionDB(policyFunctionName);
            PolicyFunctionSerialisationEngine.serialise(policyFunctionName, clusterName, sm_db);


            //PolicyFunctionDB sm_db = new PolicyFunctionDB();
            //sm_db.clusterName = clusterName;
            //sm_db.functionName = policyFunctionName;

            //PolicyFunctionDB function_exist = findPolicyFunction(policyFunctionName, clusterName);
            //if (function_exist == null)
            //{
            //    db.PolicyFunctionDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addStateMachineStartStateDB(string startState, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(startState))
                {
                    smDB.stateMachine.startState = state;
                    break;
                }
            }
            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //StateMachineStartStateDB sm_db = new StateMachineStartStateDB();
            //sm_db.clusterName = clusterName;
            //sm_db.machineName = machineName;
            //sm_db.startState = startState;

            //StateMachineStartStateDB startState_exist = findStateMachineStartState(machineName, clusterName);
            //if (startState_exist == null)
            //{
            //    db.StateMachineStartStateDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateStateMachineStartStateDB(startState, machineName, clusterName);
            //}
        }

        public static void addStateMachineEndStateDB(string endState, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(endState))
                {
                    smDB.stateMachine.endState = state;
                    break;
                }
            }
            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //StateMachineEndStateDB sm_db = new StateMachineEndStateDB();
            //sm_db.clusterName = clusterName;
            //sm_db.machineName = machineName;
            //sm_db.endState = endState;

            //StateMachineEndStateDB endState_exist = findStateMachineEndState(machineName, clusterName);
            //if (endState_exist == null)
            //{
            //    db.StateMachineEndStateDBs.InsertOnSubmit(sm_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateStateMachineEndStateDB(endState, machineName, clusterName);
            //}
        }

        private static void updateStateMachineStartStateDB(string startState, string machineName, string clusterName)
        {
            addStateMachineStartStateDB(startState, machineName, clusterName);

            //var mc_ = (from m in db.StateMachineStartStateDBs
            //           where m.machineName == machineName
            //           && m.clusterName == clusterName
            //           select m).SingleOrDefault<StateMachineStartStateDB>();
            //if (mc_ != null)
            //{
            //    mc_.startState = startState;
            //    mc_.machineName = machineName;
            //    mc_.clusterName = clusterName;
            //    db.SubmitChanges();
            //}
        }

        private static void updateStateMachineEndStateDB(string endState, string machineName, string clusterName)
        {
            addStateMachineEndStateDB(endState, machineName, clusterName);

            //var mc_ = (from m in db.StateMachineEndStateDBs
            //           where m.machineName == machineName
            //           && m.clusterName == clusterName
            //           select m).SingleOrDefault<StateMachineEndStateDB>();
            //if (mc_ != null)
            //{
            //    mc_.endState = endState;
            //    mc_.machineName = machineName;
            //    mc_.clusterName = clusterName;
            //    db.SubmitChanges();
            //}
        }

        public static void addStateDB(State state, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, state.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(state.machineName, clusterName);
            smDB.stateMachine.addState(state);

            StateMachineSerialisationEngine.serialise(state.machineName, clusterName, smDB);

            //addStateMachineDB(state.machineName, clusterName);

            //StateDB s_db = new StateDB();
            //s_db.machineName = state.machineName;
            //s_db.stateName = state.name;
            //s_db.stateDescription = state.description;
            //s_db.clusterName = clusterName;

            //if (state.isMitigationPoint == true)
            //{
            //    s_db.isMitigationPoint = "true";
            //}
            //if (state.isMisusePoint == true)
            //{
            //    s_db.isMisusePoint = "true";
            //}
            //if (state.isCallable == true)
            //{
            //    s_db.isCallable = "true";
            //}
            //else
            //{
            //    s_db.isCallable = "false";
            //}

            //StateDB state_exist = findState(state.machineName, state.name, clusterName);
            //if (state_exist == null)
            //{
            //    db.StateDBs.InsertOnSubmit(s_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addStateDB(string machineName, string stateName, string desc, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            bool exist = false;
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    exist = true;
                    break;
                }
            }
            if (exist == true)
            {
                return;
            }

            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);

            State sadd = new State();
            sadd.machineName = machineName;
            sadd.name = stateName;
            sadd.description = desc;
            sadd.clusterName = clusterName;

            smDB.stateMachine.addState(sadd);

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //addStateMachineDB(machineName, clusterName);
            //StateDB s_db = new StateDB();
            //s_db.machineName = machineName;
            //s_db.stateName = stateName;
            //s_db.stateDescription = desc;
            //s_db.clusterName = clusterName;

            //StateDB state_exist = findState(machineName, stateName, clusterName);
            //if (state_exist == null)
            //{
            //    db.StateDBs.InsertOnSubmit(s_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addStateTransitionDB(Transition transition, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, transition.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(transition.machineName, clusterName);
            smDB.stateMachine.addStateTransition(transition);

            StateMachineSerialisationEngine.serialise(transition.machineName, clusterName, smDB);


            //foreach (int policyID in transition.policyIDs)
            //{
            //    StateTransitionDB st_db = new StateTransitionDB();
            //    st_db.machineName = transition.machineName;
            //    st_db.transitionName = transition.transitionName;
            //    st_db.startState = transition.startState;
            //    st_db.endState = transition.endState;
            //    st_db.associatedEvent = transition.associatedEevent;
            //    st_db.policyID = policyID;
            //    st_db.clusterName = clusterName;
            //    if (transition.isCallable == true)
            //    {
            //        st_db.isCallable = "true";
            //    }
            //    if (transition.isMitigationPoint == true)
            //    {
            //        st_db.isMitigationPoint = "true";
            //    }
            //    if (transition.isMisusePoint == true)
            //    {
            //        st_db.isMisusePoint = "true";
            //    }
            //    StateTransitionDB stateTransition_exist = findStateTransition(transition, policyID, clusterName);
            //    if (stateTransition_exist == null)
            //    {
            //        db.StateTransitionDBs.InsertOnSubmit(st_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateStateTransitionDB(transition, clusterName);
            //    }

            //}
            //filterStateTransitionPolicies(transition, clusterName);
        }


        //private static void filterStateTransitionPolicies(Transition transition, string clusterName)
        //{
        //    var eoa_dbs =
        //        (from evoa_db in db.StateTransitionDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.transitionName == transition.transitionName
        //         && evoa_db.machineName == transition.machineName
        //         && evoa_db.startState == transition.startState
        //         && evoa_db.endState == transition.endState
        //         && evoa_db.associatedEvent == transition.associatedEevent
        //         select evoa_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        bool valid = false;
        //        foreach (int val_oa in transition.policyIDs)
        //        {
        //            if (val_oa == eoa_db.policyID)
        //            {
        //                valid = true;
        //                break;
        //            }
        //        }
        //        if (valid == false)
        //        {
        //            db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //            db.SubmitChanges();
        //        }
        //    }
        //} 

        //private static void filterEventTriggerDB(Event event_, string clusterName)
        //{

        //    var eoa_dbs =
        //        (from evoa_db in db.EventTriggerDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.eventName == event_.name 
        //         && evoa_db.machineName == event_.machineName
        //         select evoa_db);

        //    foreach (EventTriggerDB eoa_db in eoa_dbs)
        //    {
        //        bool valid = false;
        //        foreach (string val_oa in event_.triggers)
        //        {
        //            if (val_oa.Equals(eoa_db.eventTrigger))
        //            {
        //                valid = true;
        //                break;
        //            }
        //        }
        //        if (valid == false)
        //        {
        //            db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
        //            db.SubmitChanges();
        //        }
        //    }
        //}

        //private static void filterEventOperationalAttributeDB(Event event_, string clusterName)
        //{

        //    var eoa_dbs =
        //        (from evoa_db in db.EventOperationalAttributeDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.eventName == event_.name
        //         && evoa_db.machineName == event_.machineName
        //         select evoa_db);

        //    foreach (EventOperationalAttributeDB eoa_db in eoa_dbs)
        //    {
        //        bool valid = false;
        //        foreach (string val_oa in event_.operationalAttributes_)
        //        {
        //            if (val_oa.Equals(eoa_db.operationalAttribute))
        //            {
        //                valid = true;
        //                break;
        //            }
        //        }
        //        if (valid == false)
        //        {
        //            db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //            db.SubmitChanges();
        //        }
        //    }
        //}

        //private static void filterStateActionOperationalAttributeDB(PMisuseAn.behaviour.model.Action action_, string clusterName)
        //{

        //    var eoa_dbs =
        //        (from evoa_db in db.StateActionOperationalAttributeDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.actionName == action_.name
        //         && evoa_db.machineName == action_.machineName
        //         select evoa_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in eoa_dbs)
        //    {
        //        bool valid = false;
        //        foreach (string val_oa in action_.operationalAttributes_)
        //        {
        //            if (val_oa.Equals(eoa_db.operationalAttribute))
        //            {
        //                valid = true;
        //                break;
        //            }
        //        }
        //        if (valid == false)
        //        {
        //            db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //            db.SubmitChanges();
        //        }
        //    }
        //} 

        public static void deleteEventTriggerDB(string eventName, string triggerName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (Event eve in smDB.stateMachine.events)
            {
                if (eve.name.Equals(eventName))
                {
                    foreach (string trigger in eve.triggers)
                    {
                        if (trigger.Equals(triggerName))
                        {
                            eve.removeTrigger(triggerName);
                            break;
                        }
                    }
                    break;
                }
            }
            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //var eoa_dbs =
            //    (from evoa_db in db.EventTriggerDBs
            //     where evoa_db.clusterName == clusterName
            //     && evoa_db.eventName == eventName
            //     && evoa_db.eventTrigger == triggerName
            //     && evoa_db.machineName == machineName
            //     select evoa_db);

            //foreach (EventTriggerDB eoa_db in eoa_dbs)
            //{
            //    db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        //public static void deleteEventTriggerDB(string triggerName, string clusterName)
        //{
        //    var eoa_dbs =
        //        (from evoa_db in db.EventTriggerDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.eventTrigger == triggerName
        //         select evoa_db);

        //    foreach (EventTriggerDB eoa_db in eoa_dbs)
        //    {
        //        db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void deleteEventTriggerDBSM(string machineName, string clusterName)
        //{
        //    var eoa_dbs =
        //        (from evoa_db in db.EventTriggerDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.machineName == machineName
        //         select evoa_db);

        //    foreach (EventTriggerDB eoa_db in eoa_dbs)
        //    {
        //        db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void deleteEventTriggerDBSM(string clusterName)
        //{
        //    var eoa_dbs =
        //        (from evoa_db in db.EventTriggerDBs
        //         where evoa_db.clusterName == clusterName
        //         select evoa_db);

        //    foreach (EventTriggerDB eoa_db in eoa_dbs)
        //    {
        //        db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteEventDBSM(string machineName, string clusterName)
        //{
        //    deleteEventTriggerDBSM(machineName, clusterName);
        //    deleteEventOperationalAttributeDB(machineName, clusterName);
        //    deleteStateTransitionDBSM(machineName, clusterName);
        //    var eoa_dbs =
        //        (from ev_db in db.EventDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.machineName == machineName
        //         select ev_db);

        //    foreach (EventDB eoa_db in eoa_dbs)
        //    {
        //        db.EventDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteEventDBSM(string clusterName)
        //{
        //    deleteEventTriggerDBSM(clusterName);
        //    deleteEventOperationalAttributeDB(clusterName);
        //    deleteStateTransitionDBSM(clusterName);
        //    var eoa_dbs =
        //        (from ev_db in db.EventDBs
        //         where ev_db.clusterName == clusterName
        //         select ev_db);

        //    foreach (EventDB eoa_db in eoa_dbs)
        //    {
        //        db.EventDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteEventTriggerDB(string machineName,string eventName, string clusterName)
        //{
        //    var eoa_dbs =
        //        (from evoa_db in db.EventTriggerDBs
        //         where evoa_db.clusterName == clusterName
        //         && evoa_db.eventName == eventName
        //         && evoa_db.machineName == machineName
        //         select evoa_db);

        //    foreach (EventTriggerDB eoa_db in eoa_dbs)
        //    {
        //        db.EventTriggerDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteEventDB(string machineName, string eventName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            smDB.stateMachine.removeEvent(eventName);

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //deleteEventTriggerDB(machineName, eventName, clusterName);
            //deleteEventOperationalAttributeDB(eventName, machineName, clusterName);
            //deleteStateTransitionDBEvent(machineName, eventName, clusterName);

            //var eoa_dbs =
            //    (from ev_db in db.EventDBs
            //     where ev_db.clusterName == clusterName
            //     && ev_db.machineName == machineName && ev_db.eventName == eventName
            //     select ev_db);

            //foreach (EventDB eoa_db in eoa_dbs)
            //{
            //    db.EventDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        //private static void deleteStateTransitionDBSM(string machineName, string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    trans.machineName = machineName;
        //    deleteStateTransitionMitigationReferenceDBCNMN(trans); 

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.machineName == machineName
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void deleteStateTransitionDBSM(string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    deleteStateTransitionMitigationReferenceDBCN(trans); 

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}


        //public static void deleteStateTransitionDBTrans(string machineName, string transitionName, string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    trans.machineName = machineName;
        //    trans.transitionName = transitionName;
        //    deleteStateTransitionMitigationReferenceDBCNMNTN(trans); 

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.transitionName == transitionName
        //         && ev_db.machineName == machineName
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateTransitionDBEvent(string machineName, string eventName, string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    trans.machineName = machineName;
        //    trans.associatedEevent = eventName;

        //    deleteStateTransitionMitigationReferenceDBCNMNEN(trans); 

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.machineName == machineName && ev_db.associatedEvent == eventName
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        ////public static void deleteStateTransitionDBPolicy(string machineName, string eventName,
        ////    string startState, string endState, string transitionName, int policyID, string clusterName)
        ////{
        ////    var eoa_dbs =
        ////        (from ev_db in db.StateTransitionDBs
        ////         where ev_db.clusterName == clusterName
        ////         && ev_db.machineName == machineName 
        ////         && ev_db.associatedEvent == eventName
        ////          && ev_db.startState == startState
        ////           && ev_db.endState == endState
        ////            && ev_db.transitionName == transitionName
        ////             && ev_db.policyID == policyID
        ////         select ev_db);

        ////    foreach (StateTransitionDB eoa_db in eoa_dbs)
        ////    {
        ////        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        ////        db.SubmitChanges();
        ////    }
        ////}
        //private static void deleteStateTransitionDBPolicy(int policyID, string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    deleteStateTransitionMitigationReferenceDBCNPN(trans, policyID); 


        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.policyID == policyID
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //private static void updateStateTransitionDBPolicy(int policyID, string clusterName)
        //{
        //    Policy nullpolicy = loadDoNothingPolicy(clusterName);
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.policyID == policyID
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        eoa_db.policyID = nullpolicy.id;
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateTransitionDBState(string machineName,string state, string clusterName)
        //{
        //    Transition trans = new Transition();
        //    trans.clusterName = clusterName;
        //    trans.machineName = machineName;
        //    trans.endState = state;
        //    trans.startState = state;
        //    deleteStateTransitionMitigationReferenceDBCNMNSN(trans); 

        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.machineName == machineName && 
        //         (ev_db.endState == state || ev_db.startState == state)
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteStateTransitionDBTransStateEvent(Transition transition, string clusterName)
        {
            StateMachineDB stateMachineDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, transition.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(transition.machineName, clusterName);
            stateMachineDB.stateMachine.removeStateTransition(transition.transitionName);
            StateMachineSerialisationEngine.serialise(transition.machineName, clusterName, stateMachineDB);

            //transition.clusterName = clusterName;
            ////deleteStateTransitionMitigationReferenceDBCNMNTNSE(transition);

            //var eoa_dbs =
            //    (from ev_db in db.StateTransitionDBs
            //     where ev_db.clusterName == clusterName
            //        && ev_db.machineName == transition.machineName
            //        && ev_db.endState == transition.endState
            //        && ev_db.startState == transition.startState
            //        && ev_db.associatedEvent == transition.associatedEevent
            //        && ev_db.transitionName == transition.transitionName
            //     select ev_db);

            //foreach (StateTransitionDB eoa_db in eoa_dbs)
            //{
            //    db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteStateMachineDB(string stateMachine, string clusterName)
        {
            //deleteGroupAgentDBMachine(stateMachine, clusterName);

            StateMachineSerialisationEngine.deleteStateMachineDB(stateMachine, clusterName);


            //State s = new State();
            //s.clusterName = clusterName;
            //s.machineName = stateMachine;
            //deleteStateMitigationReferenceDBCNMN(s); 

            //deleteStateMachineStartStateDBSM(stateMachine, clusterName);
            //deleteStateMachineEndStateDBSM(stateMachine, clusterName);
            //deleteStateTransitionDBSM(stateMachine, clusterName);
            //deleteEventTriggerDBSM(stateMachine, clusterName);
            //deleteEventDBSM(stateMachine, clusterName);
            //deleteStateActionOperationalAttributeDBSM(stateMachine, clusterName);
            //deleteStateActionDBSM(stateMachine, clusterName);
            //deleteStateDBSM(stateMachine, clusterName);

            //var sm_dbs =
            //    (from sm_db in db.StateMachineDBs
            //     where sm_db.clusterName == clusterName
            //     && sm_db.machineName == stateMachine
            //     select sm_db);

            //foreach (StateMachineDB eoa_db in sm_dbs)
            //{
            //    db.StateMachineDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        private static void deleteStateMachineDB(string clusterName)
        {
            List<string> sms = StateMachineSerialisationEngine.getStateMachineList(clusterName);
            foreach (string sm in sms)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(clusterName, sm);
            }

            //State s = new State();
            //s.clusterName = clusterName;
            //deleteStateMitigationReferenceDBCN(s); 

            //deleteStateMachineStartStateDBSM(clusterName);
            //deleteStateMachineEndStateDBSM(clusterName);
            //deleteStateTransitionDBSM(clusterName);
            //deleteEventTriggerDBSM(clusterName);
            //deleteEventDBSM(clusterName);
            //deleteStateActionOperationalAttributeDBSM(clusterName);
            //deleteStateActionDBSM(clusterName);
            //deleteStateDBSM(clusterName);

            //var sm_dbs =
            //    (from sm_db in db.StateMachineDBs
            //     where sm_db.clusterName == clusterName
            //     select sm_db);

            //foreach (StateMachineDB eoa_db in sm_dbs)
            //{
            //    db.StateMachineDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        //public static void deleteStateMachineStartStateDBSM(string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineStartStateDBs
        //         where sm_db.clusterName == clusterName
        //         select sm_db);

        //    foreach (StateMachineStartStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineStartStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateMachineStartStateDBSM(string machineName, string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineStartStateDBs
        //         where sm_db.clusterName == clusterName
        //         && sm_db.machineName == machineName
        //         select sm_db);

        //    foreach (StateMachineStartStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineStartStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateMachineStartStateDBSM(string startState, string machineName, string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineStartStateDBs
        //         where sm_db.startState == startState
        //         && sm_db.clusterName == clusterName
        //         && sm_db.machineName == machineName
        //         select sm_db);

        //    foreach (StateMachineStartStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineStartStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateMachineEndStateDBSM(string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineEndStateDBs
        //         where sm_db.clusterName == clusterName
        //         select sm_db);

        //    foreach (StateMachineEndStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineEndStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateMachineEndStateDBSM(string machineName, string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineEndStateDBs
        //         where sm_db.clusterName == clusterName
        //         && sm_db.machineName == machineName
        //         select sm_db);

        //    foreach (StateMachineEndStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineEndStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateMachineEndStateDBSM(string endState, string machineName, string clusterName)
        //{
        //    var sm_dbs =
        //        (from sm_db in db.StateMachineEndStateDBs
        //         where sm_db.endState == endState
        //         && sm_db.clusterName == clusterName
        //         && sm_db.machineName == machineName
        //         select sm_db);

        //    foreach (StateMachineEndStateDB eoa_db in sm_dbs)
        //    {
        //        db.StateMachineEndStateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static string getStartState(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB != null && smDB.stateMachine.startState != null)
            {
                return smDB.stateMachine.startState.name;
            }
            else
                return null;

            //var st = (from s_ in db.StateMachineStartStateDBs
            //           where s_.machineName == machineName
            //           && s_.clusterName == clusterName
            //          select s_).SingleOrDefault<StateMachineStartStateDB>();

            //if (st != null)
            //{
            //    return st.startState;
            //}
            //return null;
        }

        public static string getEndState(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB != null && smDB.stateMachine.endState != null)
            {
                return smDB.stateMachine.endState.name;
            }
            else
                return null;

            //var st = (from s_ in db.StateMachineEndStateDBs
            //          where s_.machineName == machineName
            //          && s_.clusterName == clusterName
            //          select s_).SingleOrDefault<StateMachineEndStateDB>();

            //if (st != null)
            //{
            //    return st.endState;
            //}
            //return null;
        }

        public static void deleteClusterDB(string clusterName)
        {
            //deleteGroupAgentDBCluster(clusterName);
            deleteGroupDBCluster(clusterName);
            //deleteEventOperationalAttributeDB(clusterName);
            //deleteStateActionOperationalAttributeDBSM(clusterName);
            //deleteStateActionDBSM(clusterName);
            //deleteStateDBSM(clusterName);
            deleteAttribute(clusterName);
            deleteStateMachineDB(clusterName);

            DomainClusterSerialisationEngine.deleteDomainClusterDB(clusterName);

            //var sm_dbs =
            //    (from sm_db in db.DomainClusterDBs
            //     where sm_db.clusterName == clusterName
            //     select sm_db);

            //foreach (DomainClusterDB eoa_db in sm_dbs)
            //{
            //    db.DomainClusterDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteGroupFunctionDB(string groupFunctionName, string clusterName)
        {
            GroupFunctionSerialisationEngine.deleteGroupFunctionDB(groupFunctionName, clusterName);

            //var sm_dbs =
            //    (from sm_db in db.GroupFunctionDBs
            //     where sm_db.clusterName == clusterName
            //     && sm_db.functionName == groupFunctionName
            //     select sm_db);

            //foreach (GroupFunctionDB eoa_db in sm_dbs)
            //{
            //    db.GroupFunctionDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deletePolicyFunctionDB(string policyFunctionName, string clusterName)
        {
            PolicyFunctionSerialisationEngine.deletePolicyFunctionDB(policyFunctionName, clusterName);

            //var sm_dbs =
            //    (from sm_db in db.PolicyFunctionDBs
            //     where sm_db.clusterName == clusterName
            //     && sm_db.functionName == policyFunctionName
            //     select sm_db);

            //foreach (PolicyFunctionDB eoa_db in sm_dbs)
            //{
            //    db.PolicyFunctionDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }


        //public static void deleteStateDBSM(string clusterName)
        //{
        //    State s = new State();
        //    s.clusterName = clusterName;
        //    deleteStateMitigationReferenceDBCN(s); 

        //    deleteStateMachineStartStateDBSM(clusterName);
        //    deleteStateMachineEndStateDBSM(clusterName);
        //    deleteStateActionOperationalAttributeDBSM(clusterName);
        //    deleteStateActionDBSM(clusterName);

        //    var s_dbs =
        //        (from s_db in db.StateDBs
        //         where s_db.clusterName == clusterName
        //         select s_db);

        //    foreach (StateDB eoa_db in s_dbs)
        //    {
        //        db.StateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateDBSM(string machineName, string clusterName)
        //{
        //    State s = new State();
        //    s.clusterName = clusterName;
        //    s.machineName = machineName;
        //    deleteStateMitigationReferenceDBCNMN(s); 

        //    deleteStateMachineStartStateDBSM(machineName, clusterName);
        //    deleteStateMachineEndStateDBSM(machineName, clusterName);
        //    deleteStateActionOperationalAttributeDBSM(machineName, clusterName);
        //    deleteStateActionDBSM(machineName, clusterName);

        //    var s_dbs =
        //        (from s_db in db.StateDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName
        //         select s_db);

        //    foreach (StateDB eoa_db in s_dbs)
        //    {
        //        db.StateDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionDBSM(string machineName, string clusterName)
        //{
        //    deleteStateActionOperationalAttributeDBSM(machineName, clusterName);
        //    PMisuseAn.behaviour.model.Action a = new PMisuseAn.behaviour.model.Action();
        //    //a.name = actionName;
        //    //a.stateName = stateName;
        //    a.machineName = machineName;
        //    a.clusterName = clusterName;
        //    deleteStateActionMitigationReferenceDBXSs(a);

        //    var s_dbs =
        //        (from s_db in db.StateActionDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName
        //         select s_db);

        //    foreach (StateActionDB eoa_db in s_dbs)
        //    {
        //        db.StateActionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionDBSM(string clusterName)
        //{
        //    deleteStateActionOperationalAttributeDBSM(clusterName);
        //    PMisuseAn.behaviour.model.Action a = new PMisuseAn.behaviour.model.Action();
        //    //a.name = actionName;
        //    //a.stateName = stateName;
        //    //a.machineName = machineName;
        //    a.clusterName = clusterName;
        //    deleteStateActionMitigationReferenceDBXSM(a);

        //    var s_dbs =
        //        (from s_db in db.StateActionDBs
        //         where s_db.clusterName == clusterName
        //         select s_db);

        //    foreach (StateActionDB eoa_db in s_dbs)
        //    {
        //        db.StateActionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}


        //public static void deleteStateActionOperationalAttributeDBSM(string machineName, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionOperationalAttributeDBSM(string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionOperationalAttributeDBatt(string operationalAttribute, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.operationalAttribute == operationalAttribute
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteEventOperationalAttributeDBatt(string operationalAttribute, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.EventOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.operationalAttribute == operationalAttribute
        //         select s_db);

        //    foreach (EventOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteEventOperationalAttributeDB(string eventName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            smDB.stateMachine.removeEvent(eventName);

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //var s_dbs =
            //    (from s_db in db.EventOperationalAttributeDBs
            //     where s_db.clusterName == clusterName
            //     && s_db.machineName == machineName
            //     && s_db.eventName == eventName
            //     select s_db);

            //foreach (EventOperationalAttributeDB eoa_db in s_dbs)
            //{
            //    db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }


        //public static void deleteEventOperationalAttributeDB(string machineName, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.EventOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName
        //         select s_db);

        //    foreach (EventOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteEventOperationalAttributeDB(string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.EventOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         select s_db);

        //    foreach (EventOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionOperationalAttributeDBatt(string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteStateDB(string machineName, string stateName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            smDB.stateMachine.removeState(stateName);

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //State s = new State();
            //s.machineName = machineName;
            //s.clusterName = clusterName;
            //s.name = stateName;
            //deleteStateMitigationReferenceDB(s);  

            //deleteStateMachineStartStateDBSM(stateName, machineName, clusterName);
            //deleteStateMachineEndStateDBSM(stateName, machineName, clusterName);
            //deleteStateActionOperationalAttributeDB(machineName, stateName, clusterName);
            //deleteStateTransitionDBState(machineName, stateName, clusterName);
            //deleteStateActionDB(machineName, stateName, clusterName);

            //var s_dbs =
            //    (from s_db in db.StateDBs
            //     where s_db.clusterName == clusterName
            //     && s_db.machineName == machineName 
            //     && s_db.stateName == stateName
            //     select s_db);

            //foreach (StateDB eoa_db in s_dbs)
            //{
            //    db.StateDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        //public static void deleteStateActionDB(string machineName, string stateName, string clusterName)
        //{
        //    deleteStateActionOperationalAttributeDB(machineName, stateName, clusterName);
        //    PMisuseAn.behaviour.model.Action a = new PMisuseAn.behaviour.model.Action();
        //    //a.name = actionName;
        //    a.stateName = stateName;
        //    a.machineName = machineName;
        //    a.clusterName = clusterName;
        //    deleteStateActionMitigationReferenceDBXS(a);

        //    var s_dbs =
        //        (from s_db in db.StateActionDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName 
        //         && s_db.stateName == stateName
        //         select s_db);

        //    foreach (StateActionDB eoa_db in s_dbs)
        //    {
        //        db.StateActionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteStateActionDB(string machineName, string stateName, string actionName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    foreach (PMisuseAn.behaviour.model.Action act in state.actions)
                    {
                        if (act.name.Equals(actionName))
                        {
                            state.removeAction(actionName);
                            break;
                        }
                    }
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);


            //deleteStateActionOperationalAttributeDB(machineName, stateName, actionName, clusterName);

            //PMisuseAn.behaviour.model.Action a = new PMisuseAn.behaviour.model.Action();
            //a.name = actionName;
            //a.stateName = stateName;
            //a.machineName = machineName;
            //a.clusterName = clusterName;
            //deleteStateActionMitigationReferenceDB(a);

            //var s_dbs =
            //    (from s_db in db.StateActionDBs
            //     where s_db.clusterName == clusterName
            //     && s_db.machineName == machineName 
            //     && s_db.stateName == stateName
            //     && s_db.name == actionName
            //     select s_db);

            //foreach (StateActionDB eoa_db in s_dbs)
            //{
            //    db.StateActionDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        //public static void deleteStateActionOperationalAttributeDB(string machineName, string stateName, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName 
        //         && s_db.stateName == stateName
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        //public static void deleteStateActionOperationalAttributeDB(string machineName, string stateName, string actionName, string clusterName)
        //{
        //    var s_dbs =
        //        (from s_db in db.StateActionOperationalAttributeDBs
        //         where s_db.clusterName == clusterName
        //         && s_db.machineName == machineName 
        //         && s_db.stateName == stateName
        //         && s_db.actionName == actionName
        //         select s_db);

        //    foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
        //    {
        //        db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void deleteStateActionOperationalAttributeDB(string machineName, string stateName, string actionName, string operationalAttribute, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    foreach (PMisuseAn.behaviour.model.Action act in state.actions)
                    {
                        if (act.name.Equals(actionName))
                        {
                            act.removeOperationalAttribute(operationalAttribute);
                            break;
                        }
                    }
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //var s_dbs =
            //    (from s_db in db.StateActionOperationalAttributeDBs
            //     where s_db.clusterName == clusterName
            //     && s_db.machineName == machineName
            //     && s_db.stateName == stateName
            //     && s_db.actionName == actionName
            //     && s_db.operationalAttribute == operationalAttribute
            //     select s_db);

            //foreach (StateActionOperationalAttributeDB eoa_db in s_dbs)
            //{
            //    db.StateActionOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void deleteEventOperationalAttributeDB(string operationalAttribute, string eventName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (Event eve in smDB.stateMachine.events)
            {
                if (eve.name.Equals(eventName))
                {
                    foreach (string att in eve.operationalAttributes_)
                    {
                        if (att.Equals(operationalAttribute))
                        {
                            eve.removeOperationalAttribute(operationalAttribute);
                            break;
                        }
                    }
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //var s_dbs =
            //    (from s_db in db.EventOperationalAttributeDBs
            //     where s_db.clusterName == clusterName
            //     && s_db.machineName == machineName
            //     && s_db.eventName == eventName
            //     && s_db.operationalAttribute == operationalAttribute
            //     select s_db);

            //foreach (EventOperationalAttributeDB eoa_db in s_dbs)
            //{
            //    db.EventOperationalAttributeDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}
        }

        private static void addEventTriggerDB(Event event_, string triggerName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, event_.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(event_.machineName, clusterName);
            foreach (Event eve in smDB.stateMachine.events)
            {
                if (eve.name.Equals(event_.name))
                {
                    eve.addTrigger(triggerName);
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(event_.machineName, clusterName, smDB);

            //EventTriggerDB event_oa_db = new EventTriggerDB();
            //event_oa_db.eventName = event_.name;
            //event_oa_db.eventTrigger = triggerName;
            //event_oa_db.machineName = event_.machineName;
            //event_oa_db.clusterName = clusterName;

            //EventTriggerDB event_oa_exist = findEventTrigger(event_.name, triggerName, event_.machineName, clusterName);
            //if (event_oa_exist == null)
            //{
            //    addStateMachineDB(event_.machineName, clusterName);

            //    db.EventTriggerDBs.InsertOnSubmit(event_oa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addStateActionDB(PMisuseAn.behaviour.model.Action action, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, action.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(action.machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(action.stateName))
                {
                    state.addAction(action);
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(action.machineName, clusterName, smDB);


            //StateActionDB s_db = new StateActionDB();
            //s_db.machineName = action.machineName;
            //s_db.stateName = action.stateName;
            //s_db.name = action.name;
            //s_db.description = action.description;
            //s_db.clusterName = clusterName;

            //if (action.isMitigationPoint == true)
            //{
            //    s_db.isMitigationPoint = "true";
            //}
            //if (action.isMisusePoint == true)
            //{
            //    s_db.isMisusePoint = "true";
            //}

            //StateActionDB state_action_exist = findStateAction(action.machineName, action.stateName, action.name, clusterName);
            //if (state_action_exist == null)
            //{
            //    db.StateActionDBs.InsertOnSubmit(s_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateStateActionDB(action, clusterName);
            //}
            //foreach(string att_s in action.operationalAttributes_)
            //{
            //    StateActionOperationalAttributeDB saoa = findStateActionOperationalAttribute(action.name, action.stateName, action.machineName, att_s, MainWindow.clusterName);

            //    if (saoa == null)
            //    {
            //        addStateActionOperationalAttributeDB(action, att_s, MainWindow.clusterName);
            //    }
            //}
            //filterStateActionOperationalAttributeDB(action, MainWindow.clusterName);
        }

        private static void addStateActionDB(string machineName, string stateName, string actionName, string desc, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    PMisuseAn.behaviour.model.Action action = new PMisuseAn.behaviour.model.Action();
                    action.machineName = machineName;
                    action.stateName = stateName;
                    action.name = actionName;
                    action.description = desc;
                    action.clusterName = clusterName;

                    state.addAction(action);
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(machineName, clusterName, smDB);

            //StateActionDB s_db = new StateActionDB();
            //s_db.machineName = machineName;
            //s_db.stateName = stateName;
            //s_db.name = actionName;
            //s_db.description = desc;
            //s_db.clusterName = clusterName;

            //StateActionDB state_action_exist = findStateAction(machineName, stateName, actionName, clusterName);
            //if (state_action_exist == null)
            //{
            //    db.StateActionDBs.InsertOnSubmit(s_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addStateActionOperationalAttributeDB(PMisuseAn.behaviour.model.Action action_, string attributeName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, action_.machineName);
            StateMachineSerialisationEngine.deleteStateMachineDB(action_.machineName, clusterName);
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(action_.stateName))
                {
                    foreach (PMisuseAn.behaviour.model.Action sa in state.actions)
                    {
                        if (sa.name.Equals(action_.name))
                        {
                            sa.addOperationalAttribute(attributeName);
                            break;
                        }
                    }
                    break;
                }
            }

            StateMachineSerialisationEngine.serialise(action_.machineName, clusterName, smDB);

            //StateActionOperationalAttributeDB state_oa_db = new StateActionOperationalAttributeDB();
            //state_oa_db.actionName = action_.name;
            //state_oa_db.stateName = action_.stateName;
            //state_oa_db.machineName = action_.machineName;
            //state_oa_db.operationalAttribute = attributeName;
            //state_oa_db.clusterName = clusterName;

            //StateActionOperationalAttributeDB state_oa_exist = 
            //    findStateActionOperationalAttribute(action_.name, action_.stateName, action_.machineName, attributeName, clusterName);
            //if (state_oa_exist == null)
            //{
            //    addStateMachineDB(action_.machineName, clusterName);
            //    addStateDB(action_.machineName,action_.stateName,null, clusterName);
            //    addStateActionDB(action_.machineName, action_.stateName, action_.name, null, clusterName);

            //    db.StateActionOperationalAttributeDBs.InsertOnSubmit(state_oa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void addEventOperationalAttributeDB(Event event_, string attributeName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, event_.machineName);
            if (smDB != null)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(event_.machineName, clusterName);
                foreach (Event eve in smDB.stateMachine.events)
                {
                    if (eve.name.Equals(event_.name))
                    {
                        eve.addOperationalAttribute(attributeName);
                        break;
                    }
                }

                StateMachineSerialisationEngine.serialise(event_.machineName, clusterName, smDB);

            }

            //EventOperationalAttributeDB event_oa_db = new EventOperationalAttributeDB();
            //event_oa_db.eventName = event_.name;
            //event_oa_db.machineName = event_.machineName;
            //event_oa_db.operationalAttribute = attributeName;
            //event_oa_db.clusterName = clusterName;

            //EventOperationalAttributeDB event_oa_exist =
            //    findEventOperationalAttribute(event_.name, event_.machineName, attributeName, clusterName);
            //if (event_oa_exist == null)
            //{
            //    addStateMachineDB(event_.machineName, clusterName);
            //    addEventDB(event_, clusterName);

            //    db.EventOperationalAttributeDBs.InsertOnSubmit(event_oa_db);
            //    db.SubmitChanges();
            //}
        }

        public static void updateEventDB(Event event_, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, event_.machineName);
            if (smDB != null)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(event_.machineName, clusterName);
                smDB.stateMachine.removeEvent(event_.name);
                smDB.stateMachine.addEvent(event_);

                StateMachineSerialisationEngine.serialise(event_.machineName, clusterName, smDB);

            }

            //var eve = (from eve_ in db.EventDBs
            //           where eve_.clusterName == clusterName
            //           && eve_.eventID == event_.eventID 
            //           && eve_.machineName == event_.machineName
            //           select eve_).SingleOrDefault<EventDB>();
            //if (eve != null)
            //{
            //    string oldEventName = eve.eventName;
            //    eve.eventName = event_.name;
            //    eve.eventDescription = event_.description;
            //    eve.eventType = event_.type;
            //    eve.machineName = event_.machineName;
            //    eve.clusterName = clusterName;

            //    db.SubmitChanges();

            //    updateEventOperationalAttributeDBEvent(oldEventName, event_.name, event_.machineName, event_.clusterName);
            //    updateEventTriggerDBEvent(oldEventName, event_.name, event_.machineName, event_.clusterName);
            //    updateStateTransitionDBEvent(oldEventName, event_.name, event_.machineName, event_.clusterName);
            //}
        }

        //public static void updateEventTriggerDBEvent(string oldEventName, string newEventName, string machineName, string clusterName)
        //{
        //    var eves = (from eve_ in db.EventTriggerDBs
        //                where eve_.clusterName == clusterName
        //                && eve_.eventName == oldEventName
        //                && eve_.machineName == machineName
        //                select eve_);

        //    foreach (var eve in eves)
        //    {
        //        eve.eventName = newEventName;
        //        db.SubmitChanges();
        //    }

        //}

        //public static void updateEventOperationalAttributeDBEvent(string oldEventName, string newEventName, string machineName, string clusterName)
        //{
        //    var eves = (from eve_ in db.EventOperationalAttributeDBs
        //               where eve_.clusterName == clusterName
        //               && eve_.eventName == oldEventName
        //               && eve_.machineName == machineName
        //               select eve_);

        //    foreach (var eve in eves)
        //    {
        //        eve.eventName = newEventName;
        //        db.SubmitChanges();
        //    }

        //}

        //public static void updateStateTransitionDBEvent(string oldEventName, string newEventName, string machineName, string clusterName)
        //{
        //    var eves = (from eve_ in db.StateTransitionDBs
        //                where eve_.clusterName == clusterName
        //                && eve_.associatedEvent == oldEventName
        //                && eve_.machineName == machineName
        //                select eve_);

        //    foreach (var eve in eves)
        //    {
        //        eve.associatedEvent = newEventName;
        //        db.SubmitChanges();
        //    }

        //}

        public static void updateStateTransitionDB(Transition transition, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, transition.machineName);
            if (smDB != null)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(transition.machineName, clusterName);
                smDB.stateMachine.removeStateTransition(transition.transitionName);
                smDB.stateMachine.addStateTransition(transition);

                StateMachineSerialisationEngine.serialise(transition.machineName, clusterName, smDB);

            }

            //deleteStateTransitionDBUpdate(transition.transitionName, transition.machineName, clusterName);
            //addStateTransitionDB(transition, clusterName);

        }

        //private static void deleteStateTransitionDBUpdate(string transitionName, string machineName, string clusterName)
        //{
        //    var eoa_dbs =
        //        (from ev_db in db.StateTransitionDBs
        //         where ev_db.clusterName == clusterName
        //         && ev_db.machineName == machineName
        //         && ev_db.transitionName == transitionName
        //         select ev_db);

        //    foreach (StateTransitionDB eoa_db in eoa_dbs)
        //    {
        //        db.StateTransitionDBs.DeleteOnSubmit(eoa_db);
        //        db.SubmitChanges();
        //    }
        //}

        public static void updateStateTransitionMisusePoint(Transition transition, string clusterName, string isMisusePoint)
        {
            //foreach (int policy_id in transition.policyIDs)
            //{
            //    var tran_ = (from tra in db.StateTransitionDBs
            //                  where tra.clusterName == clusterName
            //                  && tra.associatedEvent == transition.associatedEevent
            //                  && tra.machineName == transition.machineName
            //                  && tra.transitionName == transition.transitionName
            //                  && tra.startState == transition.startState
            //                  && tra.endState == transition.endState
            //                  && tra.policyID == policy_id
            //                  select tra).SingleOrDefault<StateTransitionDB>();
            //    if (tran_ != null)
            //    {
            //        tran_.isMisusePoint = isMisusePoint;

            //        db.SubmitChanges();
            //    }
            //}
        }

        public static void updateStateTransitionMitigationPoint(Transition transition, string clusterName, string isMitigationPoint)
        {
            //foreach (int policy_id in transition.policyIDs)
            //{
            //    var tran_ = (from tra in db.StateTransitionDBs
            //                 where tra.clusterName == clusterName
            //                 && tra.associatedEvent == transition.associatedEevent
            //                 && tra.machineName == transition.machineName
            //                 && tra.transitionName == transition.transitionName
            //                 && tra.startState == transition.startState
            //                 && tra.endState == transition.endState
            //                 && tra.policyID == policy_id
            //                 select tra).SingleOrDefault<StateTransitionDB>();
            //    if (tran_ != null)
            //    {
            //        tran_.isMitigationPoint = isMitigationPoint;

            //        db.SubmitChanges();
            //    }
            //}
        }


        public static void updateEventMitigationPoint(Event event_, string clusterName, string isMitigationPoint)
        {
            //var eve = (from eve_ in db.EventDBs
            //           where eve_.clusterName == clusterName
            //           && eve_.eventName == event_.name 
            //           && eve_.machineName == event_.machineName
            //           select eve_).SingleOrDefault<EventDB>();
            //if (eve != null)
            //{
            //    eve.isMitigationPoint = isMitigationPoint;

            //    db.SubmitChanges();
            //}
        }

        public static void updateEventMisusePoint(Event event_, string clusterName, string isMisusePoint)
        {
            //var eve = (from eve_ in db.EventDBs
            //           where eve_.clusterName == clusterName
            //           && eve_.eventName == event_.name
            //           && eve_.machineName == event_.machineName
            //           select eve_).SingleOrDefault<EventDB>();
            //if (eve != null)
            //{
            //    eve.isMisusePoint = isMisusePoint;

            //    db.SubmitChanges();
            //}
        }


        public static void updateStateDB(State state_, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, state_.machineName);
            if (smDB != null)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(state_.machineName, clusterName);
                smDB.stateMachine.removeState(state_.name);
                smDB.stateMachine.addState(state_);

                StateMachineSerialisationEngine.serialise(state_.machineName, clusterName, smDB);

            }


            //var sta = (from sta_ in db.StateDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.stateID == state_.stateID 
            //           && sta_.machineName == state_.machineName
            //           select sta_).SingleOrDefault<StateDB>();
            //if (sta != null)
            //{
            //    string old_stateName = sta.stateName;
            //    sta.stateName = state_.name;
            //    sta.stateDescription = state_.description;
            //    sta.machineName = state_.machineName;
            //    sta.clusterName = clusterName;

            //    if (state_.isCallable == true)
            //    {
            //        sta.isCallable = "true";
            //    }
            //    else
            //    {
            //        sta.isCallable = "false";
            //    }

            //    db.SubmitChanges();

            //    updateStateActionDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateActionOperationalAttributesDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateActionMitigationReferencesDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateMitigationReferencesDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateMachineStartStateDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateMachineEndStateDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);
            //    updateStateTransitionDBState(old_stateName, state_.name, state_.machineName, state_.clusterName);                
            //}           
        }

        //public static void updateStateTransitionDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        //{
        //    var stas = (from sta_ in db.StateTransitionDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.endState == oldStateName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas)
        //    {
        //        sta.endState = newStateName;
        //        db.SubmitChanges();
        //    }

        //    var stas_ = (from sta_ in db.StateTransitionDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.startState == oldStateName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas_)
        //    {
        //        sta.startState = newStateName;
        //        db.SubmitChanges();
        //    }
        //}

        //public static void updateStateMachineEndStateDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        //{
        //    var stas = (from sta_ in db.StateMachineEndStateDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.endState == oldStateName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas)
        //    {
        //        sta.endState = newStateName;
        //        db.SubmitChanges();
        //    }
        //}

        //public static void updateStateMachineStartStateDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        //{
        //    var stas = (from sta_ in db.StateMachineStartStateDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.startState == oldStateName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas)
        //    {
        //        sta.startState = newStateName;
        //        db.SubmitChanges();
        //    }
        //}

        public static void updateStateMitigationReferencesDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        {
            //var stas = (from sta_ in db.StateMitigationReferenceDBs
            //            where sta_.clusterName == clusterName
            //            && sta_.stateName == oldStateName
            //            && sta_.machineName == machineName
            //            select sta_);

            //foreach (var sta in stas)
            //{
            //    sta.stateName = newStateName;
            //    db.SubmitChanges();
            //}
        }

        public static void updateStateActionMitigationReferencesDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        {
            //var stas = (from sta_ in db.StateActionMitigationReferencesDBs
            //            where sta_.clusterName == clusterName
            //            && sta_.stateName == oldStateName
            //            && sta_.machineName == machineName
            //            select sta_);

            //foreach (var sta in stas)
            //{
            //    sta.stateName = newStateName;
            //    db.SubmitChanges();
            //}
        }

        public static void updateStateActionOperationalAttributesDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        {
            //var stas = (from sta_ in db.StateActionOperationalAttributeDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.stateName == oldStateName 
            //           && sta_.machineName == machineName
            //           select sta_);

            //foreach (var sta in stas)
            //{
            //    sta.stateName = newStateName;
            //    db.SubmitChanges();
            //}
        }

        public static void updateStateActionDBState(string oldStateName, string newStateName, string machineName, string clusterName)
        {
            //var stas = (from sta_ in db.StateActionDBs
            //            where sta_.clusterName == clusterName
            //            && sta_.stateName == oldStateName
            //            && sta_.machineName == machineName
            //            select sta_);

            //foreach (var sta in stas)
            //{
            //    sta.stateName = newStateName;
            //    db.SubmitChanges();
            //}
        }



        public static void updateStateMisusePointDB(State state_, string clusterName, string isMisusePoint)
        {
            //var sta = (from sta_ in db.StateDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.stateName == state_.name
            //           && sta_.machineName == state_.machineName
            //           select sta_).SingleOrDefault<StateDB>();
            //if (sta != null)
            //{
            //    sta.stateName = state_.name;
            //    sta.stateDescription = state_.description;
            //    sta.machineName = state_.machineName;
            //    sta.clusterName = clusterName;
            //    sta.isMisusePoint = isMisusePoint;

            //    db.SubmitChanges();
            //}
        }

        public static void updateStateMitigationPointDB(State state_, string clusterName, string isMitigationPoint)
        {
            //var sta = (from sta_ in db.StateDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.stateName == state_.name
            //           && sta_.machineName == state_.machineName
            //           select sta_).SingleOrDefault<StateDB>();
            //if (sta != null)
            //{
            //    sta.stateName = state_.name;
            //    sta.stateDescription = state_.description;
            //    sta.machineName = state_.machineName;
            //    sta.clusterName = clusterName;
            //    sta.isMitigationPoint = isMitigationPoint;

            //    db.SubmitChanges();
            //}
        }

        public static void updateStateActionDB(PMisuseAn.behaviour.model.Action action_, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, action_.machineName);
            if (smDB != null)
            {
                StateMachineSerialisationEngine.deleteStateMachineDB(action_.machineName, clusterName);
                foreach (State state in smDB.stateMachine.states)
                {
                    if (state.name.Equals(action_.stateName))
                    {
                        state.removeAction(action_.name);
                        state.addAction(action_);
                        break;
                    }
                }

                StateMachineSerialisationEngine.serialise(action_.machineName, clusterName, smDB);

            }

            //var sta = (from sta_ in db.StateActionDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.actionID == action_.actionID 
            //           && sta_.stateName == action_.stateName
            //           && sta_.machineName == action_.machineName
            //           select sta_).SingleOrDefault<StateActionDB>();
            //if (sta != null)
            //{
            //    string oldActionName = sta.name;
            //    sta.description = action_.description;
            //    sta.name = action_.name;
            //    db.SubmitChanges();

            //    updateStateActionMitigationReferencesDBAction(oldActionName, action_.name, action_.stateName, action_.machineName, MainWindow.clusterName);
            //    updateStateActionOperationalAttributesDBAction(oldActionName, action_.name, action_.stateName, action_.machineName, MainWindow.clusterName);                
            //}
        }

        //public static void updateStateActionOperationalAttributesDBAction(string oldActionName, string newActionName, string stateName, string machineName, string clusterName)
        //{
        //    var stas = (from sta_ in db.StateActionOperationalAttributeDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.stateName == stateName
        //                && sta_.actionName == oldActionName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas)
        //    {
        //        sta.actionName = newActionName;
        //        db.SubmitChanges();
        //    }
        //}

        //public static void updateStateActionMitigationReferencesDBAction(string oldActionName, string newActionName, string stateName, string machineName, string clusterName)
        //{
        //    var stas = (from sta_ in db.StateActionMitigationReferencesDBs
        //                where sta_.clusterName == clusterName
        //                && sta_.stateName == stateName
        //                && sta_.actionName == oldActionName
        //                && sta_.machineName == machineName
        //                select sta_);

        //    foreach (var sta in stas)
        //    {
        //        sta.actionName = newActionName;
        //        db.SubmitChanges();
        //    }
        //}

        public static void updateStateActionMisusePointDB(PMisuseAn.behaviour.model.Action action_, string clusterName, string isMisusePoint)
        {
            //var sta = (from sta_ in db.StateActionDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.name == action_.name
            //           && sta_.stateName == action_.stateName
            //           && sta_.machineName == action_.machineName
            //           select sta_).SingleOrDefault<StateActionDB>();
            //if (sta != null)
            //{
            //    sta.isMisusePoint = isMisusePoint;

            //    db.SubmitChanges();
            //}
        }

        public static void updateStateActionMitigationPointDB(PMisuseAn.behaviour.model.Action action_, string clusterName, string isMitigationPoint)
        {
            //var sta = (from sta_ in db.StateActionDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.name == action_.name
            //           && sta_.stateName == action_.stateName
            //           && sta_.machineName == action_.machineName
            //           select sta_).SingleOrDefault<StateActionDB>();
            //if (sta != null)
            //{
            //    sta.isMitigationPoint = isMitigationPoint;

            //    db.SubmitChanges();
            //}
        }

        public static List<Event> loadAllEvents(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<Event> events = smDB.stateMachine.events;
            return events;

            //List<Event> events = new List<Event>();
            //var eves = (from eve_ in db.EventDBs
            //            where eve_.clusterName == clusterName
            //            && eve_.machineName == machineName                       
            //            select eve_);

            //foreach (var eve_ in eves)
            //{
            //    Event event_ = new Event();
            //    event_.name = eve_.eventName;
            //    event_.description = eve_.eventDescription;
            //    event_.type = eve_.eventType;
            //    event_.machineName = machineName;
            //    event_.clusterName = clusterName;

            //    if (eve_.isMitigationPoint != null && eve_.isMitigationPoint.Equals("true"))
            //    {
            //        event_.isMitigationPoint = true;
            //    }
            //    if (eve_.isMisusePoint != null && eve_.isMisusePoint.Equals("true"))
            //    {
            //        event_.isMisusePoint = true;
            //    }

            //    events.Add(event_);
            //}
            //return events;
        }

        public static List<State> loadAllStates(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<State> states = smDB.stateMachine.states;
            return states;

            //List<State> states = new List<State>();
            //var stes = (from ste_ in db.StateDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.machineName == machineName
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    State state_ = new State();
            //    state_.name = ste_.stateName;
            //    state_.description = ste_.stateDescription;
            //    state_.machineName = machineName;
            //    state_.clusterName = clusterName;
            //    state_.stateID = ste_.stateID;

            //    if (ste_.isMitigationPoint != null && ste_.isMitigationPoint.Equals("true"))
            //    {
            //        state_.isMitigationPoint = true;
            //    }

            //    if (ste_.isMisusePoint != null && ste_.isMisusePoint.Equals("true"))
            //    {
            //        state_.isMisusePoint = true;
            //    }

            //    if (ste_.isCallable != null && ste_.isCallable.Equals("true"))
            //    {
            //        state_.isCallable = true;
            //    }
            //    else
            //    {
            //        state_.isCallable = false;
            //    }

            //    List<PMisuseAn.behaviour.model.Action> actions = loadAllStateActions(machineName, state_.name, clusterName);
            //    state_.actions = actions;
            //    states.Add(state_);
            //}
            //return states;
        }

        public static List<Transition> loadAllStateTransitions(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<Transition> transitions = smDB.stateMachine.transitions;
            return transitions;


            //List<Transition> transitions = new List<Transition>();
            //var stes = (from ste_ in db.StateTransitionDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.machineName == machineName
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    Transition trans = new Transition();
            //    trans.transitionName = ste_.transitionName;
            //    trans.startState = ste_.startState;
            //    trans.endState = ste_.endState;
            //    trans.associatedEevent = ste_.associatedEvent;
            //    trans.machineName = ste_.machineName;
            //    trans.clusterName = clusterName;
            //    trans.transitionID = ste_.transitionID;
            //    if (ste_.isCallable != null && ste_.isCallable.Equals("true"))
            //    {
            //        trans.isCallable = true;
            //    }
            //    if (ste_.isMisusePoint != null && ste_.isMisusePoint.Equals("true"))
            //    {
            //        trans.isMisusePoint = true;
            //    }
            //    if (ste_.isMitigationPoint != null && ste_.isMitigationPoint.Equals("true"))
            //    {
            //        trans.isMitigationPoint = true;
            //    }

            //    bool exist = false;
            //    foreach (Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(t.clusterName)
            //           && t.transitionName.Equals(ste_.transitionName)
            //           && t.startState.Equals(ste_.startState)
            //           && t.endState.Equals(ste_.endState)
            //           && t.associatedEevent.Equals(ste_.associatedEvent)
            //           && t.machineName.Equals(ste_.machineName))
            //        {
            //            exist = true;
            //        }
            //    }
            //    if (exist == false)
            //    {
            //        transitions.Add(trans);
            //    }

            //    foreach(Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(ste_.clusterName)
            //            && t.transitionName.Equals(ste_.transitionName)
            //            && t.startState.Equals(ste_.startState)
            //            && t.endState.Equals(ste_.endState)
            //            && t.associatedEevent.Equals(ste_.associatedEvent)
            //            && t.machineName.Equals(ste_.machineName))
            //        {
            //            t.addPolicyID(ste_.policyID);
            //        }
            //    }
            //}
            //return transitions;
        }

        public static Transition loadStateTransitionDB(int transitionID, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<Transition> transitions = smDB.stateMachine.transitions;
            Transition transition = null;

            foreach (Transition trans in transitions)
            {
                if (trans.transitionID == transitionID)
                {
                    transition = trans;
                }
            }
            return transition;

            //List<Transition> transitions = new List<Transition>();
            //var stes = (from ste_ in db.StateTransitionDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.transitionID == transitionID
            //            && ste_.machineName == machineName
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    Transition trans = new Transition();
            //    trans.transitionName = ste_.transitionName;
            //    trans.startState = ste_.startState;
            //    trans.endState = ste_.endState;
            //    trans.associatedEevent = ste_.associatedEvent;
            //    trans.machineName = ste_.machineName;
            //    trans.clusterName = clusterName;
            //    trans.transitionID = ste_.transitionID;
            //    if (ste_.isCallable != null && ste_.isCallable.Equals("true"))
            //    {
            //        trans.isCallable = true;
            //    }
            //    if (ste_.isMisusePoint != null && ste_.isMisusePoint.Equals("true"))
            //    {
            //        trans.isMisusePoint = true;
            //    }
            //    if (ste_.isMitigationPoint != null && ste_.isMitigationPoint.Equals("true"))
            //    {
            //        trans.isMitigationPoint = true;
            //    }

            //    bool exist = false;
            //    foreach (Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(ste_.clusterName)
            //           && t.transitionName.Equals(ste_.transitionName)
            //           && t.startState.Equals(ste_.startState)
            //           && t.endState.Equals(ste_.endState)
            //           && t.associatedEevent.Equals(ste_.associatedEvent)
            //           && t.machineName.Equals(ste_.machineName))
            //        {
            //            exist = true;
            //        }
            //    }
            //    if (exist == false)
            //    {
            //        transitions.Add(trans);
            //    }

            //    foreach (Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(ste_.clusterName)
            //            && t.transitionName.Equals(ste_.transitionName)
            //            && t.startState.Equals(ste_.startState)
            //            && t.endState.Equals(ste_.endState)
            //            && t.associatedEevent.Equals(ste_.associatedEvent)
            //            && t.machineName.Equals(ste_.machineName))
            //        {
            //            t.addPolicyID(ste_.policyID);
            //        }
            //    }
            //}

            //if (transitions.Count == 1)
            //{
            //    return transitions.First();
            //}
            //else
            //{
            //    return null;
            //}
        }

        public static Transition loadStateTransitionDB(string machineName, string transitionName,
            string startState, string endState, string associateEvent, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;
            List<Transition> transitions = smDB.stateMachine.transitions;
            Transition transition = null;

            foreach (Transition trans in transitions)
            {
                if (trans.transitionName.Equals(transitionName) && trans.startState.Equals(startState)
                    && trans.endState.Equals(endState) && trans.associatedEevent.Equals(associateEvent))
                {
                    transition = trans;
                }
            }
            return transition;

            //List<Transition> transitions = new List<Transition>();
            //var stes = (from ste_ in db.StateTransitionDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.machineName == machineName
            //            && ste_.transitionName == transitionName
            //            && ste_.startState == startState
            //            && ste_.endState == endState
            //            && ste_.associatedEvent  == associateEvent
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    Transition trans = new Transition();
            //    trans.transitionName = ste_.transitionName;
            //    trans.startState = ste_.startState;
            //    trans.endState = ste_.endState;
            //    trans.associatedEevent = ste_.associatedEvent;
            //    trans.machineName = ste_.machineName;
            //    trans.clusterName = clusterName;
            //    trans.transitionID = ste_.transitionID;
            //    if (ste_.isCallable != null && ste_.isCallable.Equals("true"))
            //    {
            //        trans.isCallable = true;
            //    }
            //    if (ste_.isMisusePoint != null && ste_.isMisusePoint.Equals("true"))
            //    {
            //        trans.isMisusePoint = true;
            //    }
            //    if (ste_.isMitigationPoint != null && ste_.isMitigationPoint.Equals("true"))
            //    {
            //        trans.isMitigationPoint = true;
            //    }

            //    bool exist = false;
            //    foreach (Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(ste_.clusterName)
            //           && t.transitionName.Equals(ste_.transitionName)
            //           && t.startState.Equals(ste_.startState)
            //           && t.endState.Equals(ste_.endState)
            //           && t.associatedEevent.Equals(ste_.associatedEvent)
            //           && t.machineName.Equals(ste_.machineName))
            //        {
            //            exist = true;
            //        }
            //    }
            //    if (exist == false)
            //    {
            //        transitions.Add(trans);
            //    }

            //    foreach (Transition t in transitions)
            //    {
            //        if (t.clusterName.Equals(ste_.clusterName)
            //            && t.transitionName.Equals(ste_.transitionName)
            //            && t.startState.Equals(ste_.startState)
            //            && t.endState.Equals(ste_.endState)
            //            && t.associatedEevent.Equals(ste_.associatedEvent)
            //            && t.machineName.Equals(ste_.machineName))
            //        {
            //            t.addPolicyID(ste_.policyID);
            //        }
            //    }
            //}

            //if (transitions.Count == 1)
            //{                
            //    return transitions.First();
            //}
            //else
            //{
            //    return null;
            //}
        }

        public static List<PMisuseAn.behaviour.model.Action> loadAllStateActions(string machineName, string stateName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<PMisuseAn.behaviour.model.Action> actions = null;
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    actions = state.actions;
                    break;
                }
            }
            return actions;

            //List<PMisuseAn.behaviour.model.Action> actions = new List<PMisuseAn.behaviour.model.Action>();
            //var stes = (from ste_ in db.StateActionDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.machineName == machineName
            //            && ste_.stateName == stateName
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    PMisuseAn.behaviour.model.Action action = new PMisuseAn.behaviour.model.Action();
            //    action.name = ste_.name;
            //    action.stateName = ste_.stateName;
            //    action.machineName = ste_.machineName;
            //    action.description = ste_.description;
            //    action.clusterName = clusterName;
            //    action.actionID = ste_.actionID;

            //    if (ste_.isMisusePoint != null && ste_.isMisusePoint.Equals("true"))
            //    {
            //        action.isMisusePoint = true;
            //    }
            //    if (ste_.isMitigationPoint != null && ste_.isMitigationPoint.Equals("true"))
            //    {
            //        action.isMitigationPoint = true;
            //    }
            //    loadAllStateActionOperationalAttributes(machineName, stateName, action.name, clusterName);

            //    actions.Add(action);
            //}
            //return actions;
        }

        public static List<string> loadAllStateActionOperationalAttributes(string machineName, string stateName, string actionName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            List<string> operationalAttributes = null;
            foreach (State state in smDB.stateMachine.states)
            {
                if (state.name.Equals(stateName))
                {
                    foreach (PMisuseAn.behaviour.model.Action action in state.actions)
                    {
                        if (action.name.Equals(actionName))
                        {
                            operationalAttributes = action.operationalAttributes_;
                            break;
                        }
                    }
                    break;
                }
            }

            return operationalAttributes;


            //List<string> operationalAttributes = new List<string>();
            //var stes = (from ste_ in db.StateActionOperationalAttributeDBs
            //            where ste_.clusterName == clusterName
            //            && ste_.machineName == machineName
            //            && ste_.stateName == stateName
            //            && ste_.actionName == actionName
            //            select ste_);

            //foreach (var ste_ in stes)
            //{
            //    operationalAttributes.Add(ste_.operationalAttribute);
            //}
            //return operationalAttributes;
        }


        public static Event loadEventDB(string eventName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            Event event_ = null;
            foreach (Event eve in smDB.stateMachine.events)
            {
                if (eve.name.Equals(eventName))
                {
                    event_ = eve;
                }
            }

            return event_;

            //Event event_ = null;            
            //var eve = (from eve_ in db.EventDBs
            //           where eve_.clusterName == clusterName
            //           && eve_.eventName == eventName 
            //           && eve_.machineName == machineName
            //           select eve_).SingleOrDefault<EventDB>();
            //if (eve != null)
            //{
            //    event_ = new Event();
            //    event_.name = eve.eventName;
            //    event_.description= eve.eventDescription ;
            //    event_.machineName = machineName;
            //    event_.type = eve.eventType;
            //    event_.clusterName = clusterName;
            //    event_.eventID = eve.eventID;

            //    if (eve.isMitigationPoint != null && eve.isMitigationPoint.Equals("true"))
            //    {
            //        event_.isMitigationPoint = true;
            //    }
            //    if (eve.isMisusePoint != null && eve.isMisusePoint.Equals("true"))
            //    {
            //        event_.isMisusePoint = true;
            //    }

            //    setEventTriggers(event_, MainWindow.clusterName);
            //    setEventOperationalAttributes(event_, MainWindow.clusterName);
            //}

            //return event_;
        }

        //public static State loadStateDB(int stateID, string clusterName)
        //{
        //    State state_ = null;
        //    var sta = (from sta_ in db.StateDBs
        //               where sta_.clusterName == clusterName
        //               && sta_.stateID == stateID
        //               select sta_).SingleOrDefault<StateDB>();
        //    if (sta != null)
        //    {
        //        state_ = new State();
        //        state_.name = sta.stateName;
        //        state_.description = sta.stateDescription;
        //        state_.machineName = sta.machineName;
        //        state_.clusterName = clusterName;
        //        state_.stateID = sta.stateID;

        //        if (sta.isMitigationPoint != null && sta.isMitigationPoint.Equals("true"))
        //        {
        //            state_.isMitigationPoint = true;
        //        }
        //        if (sta.isMisusePoint != null && sta.isMisusePoint.Equals("true"))
        //        {
        //            state_.isMisusePoint = true;
        //        }
        //        if (sta.isCallable != null && sta.isCallable.Equals("true"))
        //        {
        //            state_.isCallable = true;
        //        }
        //        else
        //        {
        //            state_.isCallable = false;
        //        }
        //        setStateActions(state_, MainWindow.clusterName);
        //    }

        //    return state_;
        //}

        public static State loadStateDB(string stateName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            State state = null;
            foreach (State st in smDB.stateMachine.states)
            {
                if (st.name.Equals(stateName))
                {
                    state = st;
                    break;
                }
            }
            return state;

            //State state_ = null;
            //var sta = (from sta_ in db.StateDBs
            //           where sta_.clusterName == clusterName
            //           && sta_.stateName == stateName 
            //           && sta_.machineName == machineName
            //           select sta_).SingleOrDefault<StateDB>();
            //if (sta != null)
            //{
            //    state_ = new State();
            //    state_.name = sta.stateName;
            //    state_.description = sta.stateDescription;
            //    state_.machineName = machineName;
            //    state_.clusterName = clusterName;
            //    state_.stateID = sta.stateID;

            //    if (sta.isMitigationPoint != null && sta.isMitigationPoint.Equals("true"))
            //    {
            //        state_.isMitigationPoint = true;
            //    }
            //    if (sta.isMisusePoint != null && sta.isMisusePoint.Equals("true"))
            //    {
            //        state_.isMisusePoint = true;
            //    }
            //    if (sta.isCallable != null && sta.isCallable.Equals("true"))
            //    {
            //        state_.isCallable = true;
            //    }
            //    else
            //    {
            //        state_.isCallable = false;
            //    }
            //    setStateActions(state_, MainWindow.clusterName);
            //}

            //return state_;
        }

        //public static void setEventTriggers(Event event_, string clusterName)
        //{
        //    var eoatts = (from eoa_ in db.EventTriggerDBs
        //                  where eoa_.clusterName == clusterName
        //                  && eoa_.machineName == event_.machineName 
        //                  && eoa_.eventName == event_.name
        //                  select eoa_);

        //    foreach (var eoa in eoatts)
        //    {
        //        event_.addTrigger(eoa.eventTrigger);
        //    }
        //}

        //public static void setEventOperationalAttributes(Event event_, string clusterName)
        //{
        //    var eoatts = (from eoa_ in db.EventOperationalAttributeDBs
        //                  where eoa_.clusterName == clusterName
        //                  && eoa_.machineName == event_.machineName
        //                  && eoa_.eventName == event_.name
        //                  select eoa_);

        //    foreach (var eoa in eoatts)
        //    {
        //        event_.addOperationalAttribute(eoa.operationalAttribute);
        //    }
        //}

        //public static void setStateActionOperationalAttributes(PMisuseAn.behaviour.model.Action action, string clusterName)
        //{
        //    var eoatts = (from eoa_ in db.StateActionOperationalAttributeDBs
        //                  where eoa_.clusterName == clusterName
        //                  && eoa_.machineName == action.machineName
        //                  && eoa_.stateName == action.stateName
        //                  && eoa_.actionName == action.name
        //                  select eoa_);

        //    foreach (var eoa in eoatts)
        //    {
        //        action.addOperationalAttribute(eoa.operationalAttribute);
        //    }
        //}

        //public static void setStateActions(State state, string clusterName)
        //{
        //    var eoatts = (from eoa_ in db.StateActionDBs
        //                  where eoa_.clusterName == clusterName
        //                  && eoa_.machineName == state.machineName
        //                  && eoa_.stateName == state.name
        //                  select eoa_);

        //    foreach (var eoa in eoatts)
        //    {
        //        PMisuseAn.behaviour.model.Action action = new PMisuseAn.behaviour.model.Action();
        //        action.name = eoa.name;
        //        action.description = eoa.description;
        //        action.stateName = eoa.stateName;
        //        action.machineName = eoa.machineName;
        //        action.clusterName = clusterName;
        //        action.actionID = eoa.actionID;
        //        if(eoa.isMisusePoint != null && eoa.isMisusePoint.Equals("true"))
        //        {
        //            action.isMisusePoint = true;
        //        }
        //        if (eoa.isMitigationPoint != null && eoa.isMitigationPoint.Equals("true"))
        //        {
        //            action.isMitigationPoint = true;
        //        }

        //        setStateActionOperationalAttributes(action, clusterName);

        //        state.addAction(action);
        //    }
        //}

        //public static PMisuseAn.behaviour.model.Action loadStateActionDB(int actionID, string clusterName)
        //{
        //    var eoatt = (from eoa_ in db.StateActionDBs
        //                 where eoa_.clusterName == clusterName
        //                 && eoa_.actionID == actionID
        //                 select eoa_).SingleOrDefault<StateActionDB>();
        //    if (eoatt != null)
        //    {
        //        PMisuseAn.behaviour.model.Action action = new PMisuseAn.behaviour.model.Action();
        //        action.name = eoatt.name;
        //        action.description = eoatt.description;
        //        action.stateName = eoatt.stateName;
        //        action.machineName = eoatt.machineName;
        //        action.clusterName = clusterName;
        //        action.actionID = eoatt.actionID;

        //        if (eoatt.isMisusePoint != null && eoatt.isMisusePoint.Equals("true"))
        //        {
        //            action.isMisusePoint = true;
        //        }
        //        if (eoatt.isMitigationPoint != null && eoatt.isMitigationPoint.Equals("true"))
        //        {
        //            action.isMitigationPoint = true;
        //        }

        //        setStateActionOperationalAttributes(action, clusterName);

        //        return action;
        //    }
        //    return null;
        //}

        public static PMisuseAn.behaviour.model.Action loadStateActionDB(string actionName, string stateName, string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            PMisuseAn.behaviour.model.Action action = null;
            foreach (State st in smDB.stateMachine.states)
            {
                if (st.name.Equals(stateName))
                {
                    foreach (PMisuseAn.behaviour.model.Action act in st.actions)
                    {
                        if (act.name.Equals(actionName))
                        {
                            action = act;
                            break;
                        }
                    }
                    break;
                }
            }
            return action;


            //var eoatt = (from eoa_ in db.StateActionDBs
            //              where eoa_.clusterName == clusterName
            //              && eoa_.machineName == machineName
            //              && eoa_.stateName == stateName
            //              && eoa_.name == actionName
            //              select eoa_).SingleOrDefault<StateActionDB>();
            //if (eoatt != null)
            //{
            //    PMisuseAn.behaviour.model.Action action = new PMisuseAn.behaviour.model.Action();
            //    action.name = eoatt.name;
            //    action.description = eoatt.description;
            //    action.stateName = eoatt.stateName;
            //    action.machineName = eoatt.machineName;
            //    action.clusterName = clusterName;
            //    action.actionID = eoatt.actionID;

            //    if (eoatt.isMisusePoint != null && eoatt.isMisusePoint.Equals("true"))
            //    {
            //        action.isMisusePoint = true;
            //    }
            //    if (eoatt.isMitigationPoint != null && eoatt.isMitigationPoint.Equals("true"))
            //    {
            //        action.isMitigationPoint = true;
            //    }

            //    setStateActionOperationalAttributes(action, clusterName);

            //    return action;
            //}
            //return null;
        }

        public static List<string> loadAllmachines(string clusterName)
        {
            List<string> machineNames = StateMachineSerialisationEngine.getStateMachineList(clusterName);

            return machineNames;

            //List<string> machines = new List<string>();
            //var mcs = (from mc_ in db.StateMachineDBs
            //           where mc_.clusterName == clusterName
            //           select mc_);

            //foreach (var mc in mcs)
            //{
            //    machines.Add(mc.machineName);
            //}
            //return machines;
        }


        public static List<string> loadAllClusters()
        {
            List<String> domainClusers = DomainClusterSerialisationEngine.getDomainClusterList();

            return domainClusers;

            //List<string> clusters = new List<string>();
            //var mcs = (from mc_ in db.DomainClusterDBs
            //           select mc_);

            //foreach (var mc in mcs)
            //{
            //    clusters.Add(mc.clusterName);
            //}
            //return clusters;
        }


        public static List<string> loadAllGroupFunctions(string clusterName)
        {
            List<string> groupFunctions = GroupFunctionSerialisationEngine.getGroupFunctionList(clusterName);

            return groupFunctions;

            //List<string> groupFunctions = new List<string>();
            //var mcs = (from mc_ in db.GroupFunctionDBs
            //           where mc_.clusterName == clusterName
            //           select mc_);

            //foreach (var mc in mcs)
            //{
            //    groupFunctions.Add(mc.functionName);
            //}
            //return groupFunctions;
        }

        public static List<string> loadAllPolicyFunctions(string clusterName)
        {
            List<string> policyFunctions = PolicyFunctionSerialisationEngine.getPolicyFunctionList(clusterName);

            return policyFunctions;


            //List<string> policyFunctions = new List<string>();
            //var mcs = (from mc_ in db.PolicyFunctionDBs
            //           where mc_.clusterName == clusterName
            //           select mc_);

            //foreach (var mc in mcs)
            //{
            //    policyFunctions.Add(mc.functionName);
            //}
            //return policyFunctions;
        }




        public static StateMachine loadStateMachineDB(string machineName, string clusterName)
        {
            StateMachineDB smDB = StateMachineSerialisationEngine.deSerialiseActorDB(clusterName, machineName);
            if (smDB == null)
                return null;

            return smDB.stateMachine;

            //StateMachine machine = new StateMachine();
            //machine.machineName = machineName;

            //List<Event> events = loadAllEvents(machineName, clusterName);            
            //foreach (Event event_ in events)
            //{   
            //    setEventTriggers(event_, clusterName);
            //    setEventOperationalAttributes(event_, clusterName);
            //}
            //machine.events = events;

            //List<State> states = loadAllStates(machineName, clusterName);
            //foreach (State state in states)
            //{
            //    setStateActions(state, clusterName);
            //}
            //machine.states = states;

            //List<Transition> transitions = loadAllStateTransitions(machineName, clusterName);
            //machine.transitions = transitions;

            //return machine;
        }

        public static void addStateMitigationPointDB(string stateName, string machineName_, string clusterName)
        {
            //State state  = loadStateDB(stateName, machineName_,clusterName);
            //updateStateMitigationPointDB(state, clusterName, "true");
        }

        public static void removeStateMitigationPointDB(string stateName, string machineName_, string clusterName)
        {
            //State state = loadStateDB(stateName, machineName_, clusterName);
            //updateStateMitigationPointDB(state, clusterName, "");
        }

        public static void addStateMisusePointDB(string stateName, string machineName_, string clusterName)
        {
            //State state = loadStateDB(stateName, machineName_, clusterName);
            //updateStateMisusePointDB(state, clusterName, "true");
        }

        public static void removeStateMisusePointDB(string stateName, string machineName_, string clusterName)
        {
            //State state = loadStateDB(stateName, machineName_, clusterName);
            //updateStateMisusePointDB(state, clusterName, "");
            //deleteStateMitigationReferenceDB(state);
        }

        //public static int getStateID(string stateName, string machineName, string clusterName)
        //{
        //    int id = -1;
        //    var state = (from s_ in db.StateDBs
        //               where s_.clusterName == clusterName
        //               && s_.stateName == stateName
        //               && s_.machineName == machineName
        //               select s_).SingleOrDefault<StateDB>();
        //    if (state != null)
        //    {
        //        id = (int)state.stateID;
        //    }
        //    return id;
        //}

        //private static void deleteStateMitigationReferenceDBCNMN(State state)
        //{           
        //    IQueryable<StateDB> state_ids = getStateIDCNMN(state.machineName, state.clusterName);
        //    foreach (StateDB eoa_db in state_ids)
        //    {
        //        int id = eoa_db.stateID;
        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //           (from sm_db in db.StateMitigationReferenceDBs
        //            where sm_db.clusterName == state.clusterName
        //            && sm_db.machineName == state.machineName
        //            && sm_db.stateName == eoa_db.stateName
        //            select sm_db);

        //            foreach (StateMitigationReferenceDB dx in sm_dbs)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(dx);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == state.clusterName
        //                                 && ref_.machineName == state.machineName
        //                                 && ref_.referencedStateID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == state.clusterName
        //                                 && ref_.machineName == state.machineName
        //                                 && ref_.referencedStateID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == state.clusterName
        //                                     && ref_.machineName == state.machineName
        //                                     && ref_.referencedStateID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //private static void deleteStateMitigationReferenceDBCN(State state)
        //{            
        //    IQueryable<StateDB> state_ids = getStateIDCN(state.clusterName);
        //    foreach (StateDB eoa_db in state_ids)
        //    {
        //        int id = eoa_db.stateID;
        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.StateMitigationReferenceDBs
        //             where sm_db.clusterName == state.clusterName
        //             && sm_db.stateName == eoa_db.stateName
        //             && sm_db.machineName == eoa_db.machineName
        //             select sm_db);

        //            foreach (StateMitigationReferenceDB dx in sm_dbs)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(dx);
        //                db.SubmitChanges();
        //            }


        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == state.clusterName
        //                                 && ref_.machineName == state.machineName
        //                                 && ref_.referencedStateID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == state.clusterName
        //                                 && ref_.machineName == state.machineName
        //                                 && ref_.referencedStateID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == state.clusterName
        //                                     && ref_.machineName == state.machineName
        //                                     && ref_.referencedStateID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }            
        //}            

        public static void deleteStateMitigationReferenceDB(State state)
        {
            //var sm_dbs =
            //    (from sm_db in db.StateMitigationReferenceDBs
            //     where sm_db.clusterName == state.clusterName
            //     && sm_db.stateName == state.name
            //     && sm_db.machineName == state.machineName
            //     select sm_db);

            //foreach (StateMitigationReferenceDB eoa_db in sm_dbs)
            //{
            //    db.StateMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}

            //int id = getStateID(state.name, state.machineName, state.clusterName);
            //if (id > -1)
            //{
            //    var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
            //                         where ref_.clusterName == state.clusterName
            //                         && ref_.machineName == state.machineName
            //                         && ref_.referencedStateID == id
            //                         select ref_);

            //    foreach (var re in ref_ids_trans)
            //    {
            //        db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }

            //    var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
            //                         where ref_.clusterName == state.clusterName
            //                         && ref_.machineName == state.machineName
            //                         && ref_.referencedStateID == id
            //                         select ref_);
            //    foreach (var re in ref_ids_state)
            //    {
            //        db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //    var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
            //                             where ref_.clusterName == state.clusterName
            //                             && ref_.machineName == state.machineName
            //                             && ref_.referencedStateID == id
            //                             select ref_);
            //    foreach (var re in ref_ids_state_act)
            //    {
            //        db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //}
        }


        //public static void addStateActionMisusePointDB(string actionName, string stateName, string machineName_, string clusterName)
        //{
        //    PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionName, stateName, machineName_, clusterName);
        //    updateStateActionMisusePointDB(action, clusterName, "true");
        //}

        //public static void removeStateActionMisusePointDB(string actionName, string stateName, string machineName_, string clusterName)
        //{
        //    PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionName, stateName, machineName_, clusterName);
        //    updateStateActionMisusePointDB(action, clusterName, "");
        //    deleteStateActionMitigationReferenceDB(action);
        //}

        //public static int getStateActionID(string actionName, string stateName, string machineName, string clusterName)
        //{
        //    int id = -1;
        //    var state_a = (from s_ in db.StateActionDBs
        //                 where s_.clusterName == clusterName
        //                 && s_.stateName == stateName
        //                 && s_.name == actionName
        //                 && s_.machineName == machineName
        //                 select s_).SingleOrDefault<StateActionDB>();
        //    if (state_a != null)
        //    {
        //        id = (int)state_a.actionID;
        //    }
        //    return id;
        //}

        //private static IQueryable<StateActionDB> getStateActionIDMNCN(string machineName, string clusterName)
        //{
        //    var state_a_ids = (from s_ in db.StateActionDBs
        //                   where s_.clusterName == clusterName
        //                   && s_.machineName == machineName
        //                   select s_);

        //    return state_a_ids;
        //}

        //private static IQueryable<StateActionDB> getStateActionIDMNCNSN(string stateName, string machineName, string clusterName)
        //{
        //    var state_a_ids = (from s_ in db.StateActionDBs
        //                       where s_.clusterName == clusterName
        //                       && s_.machineName == machineName
        //                       && s_.stateName == stateName
        //                       select s_);

        //    return state_a_ids;
        //}

        //private static IQueryable<StateActionDB> getStateActionIDCN(string clusterName)
        //{
        //    var state_a_ids = (from s_ in db.StateActionDBs
        //                       where s_.clusterName == clusterName
        //                       select s_);

        //    return state_a_ids;
        //}

        //private static IQueryable<StateDB> getStateIDCN(string clusterName)
        //{
        //    var state_ids = (from s_ in db.StateDBs
        //                       where s_.clusterName == clusterName
        //                       select s_);

        //    return state_ids;
        //}

        //private static IQueryable<StateDB> getStateIDCNMN(string machineName, string clusterName)
        //{
        //    var state_ids = (from s_ in db.StateDBs
        //                     where s_.clusterName == clusterName
        //                     && s_.machineName == machineName
        //                     select s_);

        //    return state_ids;
        //}   

        //private static void deleteStateActionMitigationReferenceDBXS(PMisuseAn.behaviour.model.Action action)
        //{
        //    IQueryable<StateActionDB> state_a_ids = getStateActionIDMNCNSN(action.stateName, action.machineName, action.clusterName);
        //    foreach (StateActionDB eoa_db in state_a_ids)
        //    {
        //        int id = eoa_db.actionID;

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.StateActionMitigationReferencesDBs
        //             where sm_db.clusterName == action.clusterName
        //             && sm_db.stateName == action.stateName
        //             && sm_db.machineName == action.machineName
        //             && sm_db.actionName == eoa_db.name
        //             select sm_db);

        //            foreach (StateActionMitigationReferencesDB xds in sm_dbs)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(xds);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == action.clusterName
        //                                 && ref_.machineName == action.machineName
        //                                 && ref_.referencedActionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == action.clusterName
        //                                 && ref_.machineName == action.machineName
        //                                 && ref_.referencedActionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == action.clusterName
        //                                     && ref_.machineName == action.machineName
        //                                     && ref_.referencedActionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //private static void deleteStateActionMitigationReferenceDBXSM(PMisuseAn.behaviour.model.Action action)
        //{
        //    IQueryable<StateActionDB> state_a_ids = getStateActionIDCN(action.clusterName);
        //    foreach (StateActionDB eoa_db in state_a_ids)
        //    {
        //        int id = eoa_db.actionID;
        //        if (id > -1)
        //        {

        //            var sm_dbs =
        //                (from sm_db in db.StateActionMitigationReferencesDBs
        //                 where sm_db.clusterName == action.clusterName
        //                 && sm_db.actionName == eoa_db.name
        //                 && sm_db.stateName == eoa_db.stateName
        //                 && sm_db.machineName == eoa_db.machineName
        //                 select sm_db);

        //            foreach (StateActionMitigationReferencesDB dx in sm_dbs)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(dx);
        //                db.SubmitChanges();
        //            }


        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == action.clusterName
        //                                 && ref_.referencedActionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == action.clusterName
        //                                 && ref_.referencedActionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == action.clusterName
        //                                     && ref_.referencedActionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}


        //private static void deleteStateActionMitigationReferenceDBXSs(PMisuseAn.behaviour.model.Action action)
        //{
        //    IQueryable<StateActionDB> state_a_ids = getStateActionIDMNCN(action.machineName, action.clusterName);
        //    foreach (StateActionDB eoa_db in state_a_ids)
        //    {
        //         int id = eoa_db.actionID;

        //         if (id > -1)
        //         {
        //             var sm_dbs =
        //                 (from sm_db in db.StateActionMitigationReferencesDBs
        //                  where sm_db.clusterName == action.clusterName
        //                  && sm_db.machineName == action.machineName
        //                  && sm_db.actionName == eoa_db.name
        //                  && sm_db.stateName == eoa_db.stateName
        //                  select sm_db);

        //             foreach (StateActionMitigationReferencesDB dx in sm_dbs)
        //             {
        //                 db.StateActionMitigationReferencesDBs.DeleteOnSubmit(dx);
        //                 db.SubmitChanges();
        //             }

        //             var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                  where ref_.clusterName == action.clusterName
        //                                  && ref_.machineName == action.machineName
        //                                  && ref_.referencedActionID == id
        //                                  select ref_);

        //             foreach (var re in ref_ids_trans)
        //             {
        //                 db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                 db.SubmitChanges();
        //             }

        //             var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                  where ref_.clusterName == action.clusterName
        //                                  && ref_.machineName == action.machineName
        //                                  && ref_.referencedActionID == id
        //                                  select ref_);
        //             foreach (var re in ref_ids_state)
        //             {
        //                 db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                 db.SubmitChanges();
        //             }
        //             var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                      where ref_.clusterName == action.clusterName
        //                                      && ref_.machineName == action.machineName
        //                                      && ref_.referencedActionID == id
        //                                      select ref_);
        //             foreach (var re in ref_ids_state_act)
        //             {
        //                 db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                 db.SubmitChanges();
        //             }
        //         }                
        //    }       
        //}

        public static void deleteStateActionMitigationReferenceDB(PMisuseAn.behaviour.model.Action action)
        {
            //var sm_dbs =
            //    (from sm_db in db.StateActionMitigationReferencesDBs
            //     where sm_db.clusterName == action.clusterName
            //     && sm_db.stateName == action.stateName
            //     && sm_db.actionName == action.name
            //     && sm_db.machineName == action.machineName
            //     select sm_db);

            //foreach (StateActionMitigationReferencesDB eoa_db in sm_dbs)
            //{
            //    db.StateActionMitigationReferencesDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}

            //int id = getStateActionID(action.name, action.stateName, action.machineName, action.clusterName);
            //if (id > -1)
            //{
            //    var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
            //                         where ref_.clusterName == action.clusterName
            //                         && ref_.machineName == action.machineName
            //                         && ref_.referencedActionID == id
            //                         select ref_);

            //    foreach (var re in ref_ids_trans)
            //    {
            //        db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }

            //    var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
            //                         where ref_.clusterName == action.clusterName
            //                         && ref_.machineName == action.machineName
            //                         && ref_.referencedActionID == id
            //                         select ref_);
            //    foreach (var re in ref_ids_state)
            //    {
            //        db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //    var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
            //                             where ref_.clusterName == action.clusterName
            //                             && ref_.machineName == action.machineName
            //                             && ref_.referencedActionID == id
            //                             select ref_);
            //    foreach (var re in ref_ids_state_act)
            //    {
            //        db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //}
        }

        public static void addStateActionMitigationPointDB(string actionName, string stateName, string machineName_, string clusterName)
        {
            PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionName, stateName, machineName_, clusterName);
            updateStateActionMitigationPointDB(action, clusterName, "true");
        }

        public static void removeStateActionMitigationPointDB(string actionName, string stateName, string machineName_, string clusterName)
        {
            PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionName, stateName, machineName_, clusterName);
            updateStateActionMitigationPointDB(action, clusterName, "");
        }

        public static void addEventMitigationPointDB(string eventName, string machineName_, string clusterName)
        {
            Event event_ = loadEventDB(eventName, machineName_, clusterName);
            updateEventMitigationPoint(event_, clusterName, "true");
        }

        public static void removeEventMitigationPointDB(string eventName, string machineName_, string clusterName)
        {
            Event event_ = loadEventDB(eventName, machineName_, clusterName);
            updateEventMitigationPoint(event_, clusterName, "");
        }

        public static void removeEventMisusePointDB(string eventName, string machineName_, string clusterName)
        {
            Event event_ = loadEventDB(eventName, machineName_, clusterName);
            updateEventMisusePoint(event_, clusterName, "");
        }

        public static void addEventMisusePointDB(string eventName, string machineName_, string clusterName)
        {
            Event event_ = loadEventDB(eventName, machineName_, clusterName);
            updateEventMisusePoint(event_, clusterName, "true");
        }

        public static void addTransitionMisusePointDB(string transitionName_t, string startState_t, string endState_t, string associatedEvent_t, string machineName_t, string clusterName)
        {
            Transition trans = loadStateTransitionDB(machineName_t, transitionName_t, startState_t, endState_t, associatedEvent_t, clusterName);
            updateStateTransitionMisusePoint(trans, clusterName, "true");
        }

        public static void removeTransitionMisusePointDB(string transitionName_t, string startState_t, string endState_t, string associatedEvent_t, string machineName_t, string clusterName)
        {
            Transition trans = loadStateTransitionDB(machineName_t, transitionName_t, startState_t, endState_t, associatedEvent_t, clusterName);
            updateStateTransitionMisusePoint(trans, clusterName, "");
            deleteStateTransitionMitigationReferenceDB(trans);
        }

        public static void deleteStateTransitionMitigationReferenceDB(Transition trans)
        {
            //var sm_dbs =
            //    (from sm_db in db.TransitionMitigationReferenceDBs
            //     where sm_db.clusterName == trans.clusterName
            //     && sm_db.transitionName == trans.transitionName
            //     && sm_db.machineName == trans.machineName
            //     select sm_db);

            //foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
            //{
            //    db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
            //    db.SubmitChanges();
            //}

            //int id = getStateTransitionID(trans.transitionName, trans.machineName, trans.clusterName);
            //if (id > -1)
            //{
            //    var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
            //                   where ref_.clusterName == trans.clusterName
            //                   && ref_.machineName == trans.machineName
            //                   && ref_.referencedTransitionID == id
            //                   select ref_);

            //    foreach (var re in ref_ids_trans)
            //    {
            //        db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }

            //    var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
            //                         where ref_.clusterName == trans.clusterName
            //                         && ref_.machineName == trans.machineName
            //                         && ref_.referencedTransitionID == id
            //                         select ref_);
            //    foreach (var re in ref_ids_state)
            //    {
            //        db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //    var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
            //                         where ref_.clusterName == trans.clusterName
            //                         && ref_.machineName == trans.machineName
            //                         && ref_.referencedTransitionID == id
            //                         select ref_);
            //    foreach (var re in ref_ids_state_act)
            //    {
            //        db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //        db.SubmitChanges();
            //    }
            //}
        }

        //public static void deleteStateTransitionMitigationReferenceDBCNPN(Transition trans, int policyID)
        //{
        //    IQueryable<StateTransitionDB> trans_pid = 
        //        getStateTransitionIDCNMNPN(policyID,trans.clusterName);

        //    foreach(StateTransitionDB transd in trans_pid)
        //    {
        //        int id = transd.transitionID;              

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }

        //}

        //public static void deleteStateTransitionMitigationReferenceDBCNMNEN(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev = 
        //        getStateTransitionIDCNMNEN(trans.associatedEevent,trans.machineName, trans.clusterName);

        //    foreach(StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;              

        //        if (id > -1)
        //        {

        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //public static void deleteStateTransitionMitigationReferenceDBCNMNSN(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev = getStateTransitionIDCNMNSN(trans);

        //    foreach (StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.referencedTransitionID == transd.transitionID
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //public static void deleteStateTransitionMitigationReferenceDBCNMNTNSE(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev =
        //        getStateTransitionIDCNMNSE(trans);

        //    foreach (StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}


        //public static void deleteStateTransitionMitigationReferenceDBCNMN(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev =
        //        getStateTransitionIDCNMN(trans.machineName, trans.clusterName);

        //    foreach (StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //public static void deleteStateTransitionMitigationReferenceDBCNMNTN(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev =
        //        getStateTransitionIDCNMNTN(trans.transitionName,trans.machineName, trans.clusterName);

        //    foreach (StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;

        //        if (id > -1)
        //        {
        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //public static void deleteStateTransitionMitigationReferenceDBCN(Transition trans)
        //{
        //    IQueryable<StateTransitionDB> trans_ev =
        //        getStateTransitionIDCN(trans.clusterName);

        //    foreach (StateTransitionDB transd in trans_ev)
        //    {
        //        int id = transd.transitionID;

        //        if (id > -1)
        //        {

        //            var sm_dbs =
        //            (from sm_db in db.TransitionMitigationReferenceDBs
        //             where sm_db.clusterName == transd.clusterName
        //             && sm_db.machineName == transd.machineName
        //             && sm_db.transitionName == transd.transitionName
        //             select sm_db);

        //            foreach (TransitionMitigationReferenceDB eoa_db in sm_dbs)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(eoa_db);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_trans = (from ref_ in db.TransitionMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);

        //            foreach (var re in ref_ids_trans)
        //            {
        //                db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }

        //            var ref_ids_state = (from ref_ in db.StateMitigationReferenceDBs
        //                                 where ref_.clusterName == trans.clusterName
        //                                 && ref_.machineName == trans.machineName
        //                                 && ref_.referencedTransitionID == id
        //                                 select ref_);
        //            foreach (var re in ref_ids_state)
        //            {
        //                db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //            var ref_ids_state_act = (from ref_ in db.StateActionMitigationReferencesDBs
        //                                     where ref_.clusterName == trans.clusterName
        //                                     && ref_.machineName == trans.machineName
        //                                     && ref_.referencedTransitionID == id
        //                                     select ref_);
        //            foreach (var re in ref_ids_state_act)
        //            {
        //                db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
        //                db.SubmitChanges();
        //            }
        //        }
        //    }
        //}

        //public static int getStateTransitionID(string transitionName, string machineName, string clusterName)
        //{
        //    int id = -1;
        //    var trans = (from t_ in db.StateTransitionDBs
        //               where t_.clusterName == clusterName
        //               && t_.transitionName == transitionName
        //               && t_.machineName == machineName
        //               select t_).SingleOrDefault<StateTransitionDB>();
        //    if (trans != null)
        //    {
        //        id = (int)trans.transitionID;
        //    }
        //    return id;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMNEN(string associatedEventName, string machineName, string clusterName)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == clusterName
        //                 && t_.machineName == machineName
        //                 && t_.associatedEvent == associatedEventName
        //                 select t_);            
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMNSN(Transition transm)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == transm.clusterName
        //                 && t_.machineName == transm.machineName
        //                 && (t_.endState == transm.endState || t_.startState == transm.startState)
        //                 select t_);            
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMNSE(Transition transm)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == transm.clusterName
        //                 && t_.machineName == transm.machineName
        //                 && t_.endState == transm.endState
        //                 && t_.startState == transm.startState
        //                 && t_.transitionName == transm.transitionName
        //                 && t_.associatedEvent == transm.associatedEevent
        //                 select t_);            
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMNPN(int policyID,string clusterName)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == clusterName
        //                 && t_.policyID == policyID
        //                 select t_);
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCN(string clusterName)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == clusterName
        //                 select t_);
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMN(string machineName, string clusterName)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == clusterName
        //                 && t_.machineName == machineName
        //                 select t_);
        //    return trans;
        //}

        //public static IQueryable<StateTransitionDB> getStateTransitionIDCNMNTN(string transitionName, string machineName, string clusterName)
        //{
        //    var trans = (from t_ in db.StateTransitionDBs
        //                 where t_.clusterName == clusterName
        //                 && t_.machineName == machineName
        //                 && t_.transitionName == transitionName
        //                 select t_);
        //    return trans;
        //}

        public static void addTransitionMitigationPointDB(string transitionName_t, string startState_t, string endState_t, string associatedEvent_t, string machineName_t, string clusterName)
        {
            Transition trans = loadStateTransitionDB(machineName_t, transitionName_t, startState_t, endState_t, associatedEvent_t, clusterName);
            updateStateTransitionMitigationPoint(trans, clusterName, "true");
        }

        public static void removeTransitionMitigationPointDB(string transitionName_t, string startState_t, string endState_t, string associatedEvent_t, string machineName_t, string clusterName)
        {
            Transition trans = loadStateTransitionDB(machineName_t, transitionName_t, startState_t, endState_t, associatedEvent_t, clusterName);
            updateStateTransitionMitigationPoint(trans, clusterName, "");
        }

        public static MitigationReferences loadStateActionMitigationReferencesDB(string actionName, string stateName, string machineName_, string clusterName)
        {
            MitigationReferences m_ref = new MitigationReferences();

            //var refs = (from ref_ in db.StateActionMitigationReferencesDBs
            //        where ref_.clusterName == clusterName
            //        && ref_.machineName == machineName_
            //        && ref_.stateName == stateName
            //        && ref_.actionName == actionName
            //        select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        int transID = (int)re.referencedTransitionID;
            //        Transition trans = loadStateTransitionDB(transID, machineName_, clusterName);
            //        if (re.sequenceIndex != null)
            //            trans.mitigationSeqRefIndex = (int)re.sequenceIndex;
            //        m_ref.addTransition(trans);
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        int stateID = (int)re.referencedStateID;
            //        State state = loadStateDB(stateID, clusterName);
            //        if (re.sequenceIndex != null) 
            //            state.mitigationSeqRefIndex = (int)re.sequenceIndex;
            //        m_ref.addState(state);
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        int actionID = (int)re.referencedActionID;
            //        PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionID, clusterName);
            //        if (re.sequenceIndex != null) 
            //            action.mitigationSeqRefIndex = (int)re.sequenceIndex; 
            //        m_ref.addAction(action);
            //    }
            //}

            return m_ref;
        }

        public static void addStateActionMitigationReferencesDB(string actionName, string stateName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //    foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //    {
            //        StateActionMitigationReferencesDB s_db = new StateActionMitigationReferencesDB();
            //        s_db.actionName = actionName;
            //        s_db.stateName = stateName;
            //        s_db.machineName = machineName_;
            //        s_db.clusterName = clusterName;
            //        s_db.referencedActionID = action.actionID;
            //        s_db.sequenceIndex = action.mitigationSeqRefIndex;

            //        StateActionMitigationReferencesDB ref_exist = findStateActionMitigationReferenceAction(s_db);
            //        if (ref_exist == null)
            //        {
            //            db.StateActionMitigationReferencesDBs.InsertOnSubmit(s_db);
            //            db.SubmitChanges();
            //        }
            //    }

            //    foreach (State state in m_ref.states)
            //    {
            //        StateActionMitigationReferencesDB s_db = new StateActionMitigationReferencesDB();
            //        s_db.actionName = actionName;
            //        s_db.stateName = stateName;
            //        s_db.machineName = machineName_;
            //        s_db.clusterName = clusterName;
            //        s_db.referencedStateID = state.stateID;
            //        s_db.sequenceIndex = state.mitigationSeqRefIndex;

            //        StateActionMitigationReferencesDB ref_exist = findStateActionMitigationReferenceState(s_db);
            //        if (ref_exist == null)
            //        {
            //            db.StateActionMitigationReferencesDBs.InsertOnSubmit(s_db);
            //            db.SubmitChanges();
            //        }
            //    }

            //    foreach (Transition trans in m_ref.transitions)
            //    {
            //        StateActionMitigationReferencesDB s_db = new StateActionMitigationReferencesDB();
            //        s_db.actionName = actionName;
            //        s_db.stateName = stateName;
            //        s_db.machineName = machineName_;
            //        s_db.clusterName = clusterName;
            //        s_db.referencedTransitionID = trans.transitionID;
            //        s_db.sequenceIndex = trans.mitigationSeqRefIndex;

            //        StateActionMitigationReferencesDB ref_exist = findStateActionMitigationReferenceTransition(s_db);
            //        if (ref_exist == null)
            //        {
            //            db.StateActionMitigationReferencesDBs.InsertOnSubmit(s_db);
            //            db.SubmitChanges();
            //        }
            //    }
            //    filterStateActionMitigationReferences(actionName,stateName, m_ref, machineName_, clusterName);
        }

        public static void filterStateActionMitigationReferences(string actionName, string stateName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //var refs = (from ref_ in db.StateActionMitigationReferencesDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.stateName == stateName
            //            && ref_.actionName == actionName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        bool valid = false;
            //        foreach (Transition trans in m_ref.transitions)
            //        {
            //            if (re.referencedTransitionID == trans.transitionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        bool valid = false;
            //        foreach (State state in m_ref.states)
            //        {
            //            if (re.referencedStateID == state.stateID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        bool valid = false;
            //        foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //        {
            //            if (re.referencedActionID == action.actionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateActionMitigationReferencesDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //}
        }

        public static void addStateMitigationReferencesDB(string stateName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //{
            //    StateMitigationReferenceDB s_db = new StateMitigationReferenceDB();
            //    s_db.stateName = stateName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedActionID = action.actionID;
            //    s_db.sequenceIndex = action.mitigationSeqRefIndex;

            //    StateMitigationReferenceDB ref_exist = findStateMitigationReferenceAction(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.StateMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateStateMitigationReferences(s_db, action.mitigationSeqRefIndex);
            //    }
            //}

            //foreach (State state in m_ref.states)
            //{
            //    StateMitigationReferenceDB s_db = new StateMitigationReferenceDB();
            //    s_db.stateName = stateName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedStateID = state.stateID;
            //    s_db.sequenceIndex = state.mitigationSeqRefIndex;

            //    StateMitigationReferenceDB ref_exist = findStateMitigationReferenceState(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.StateMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateStateMitigationReferences(s_db, state.mitigationSeqRefIndex);
            //    }
            //}

            //foreach (Transition trans in m_ref.transitions)
            //{
            //    StateMitigationReferenceDB s_db = new StateMitigationReferenceDB();
            //    s_db.stateName = stateName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedTransitionID = trans.transitionID;
            //    s_db.sequenceIndex = trans.mitigationSeqRefIndex;

            //    StateMitigationReferenceDB ref_exist = findStateMitigationReferenceTransition(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.StateMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateStateMitigationReferences(s_db, trans.mitigationSeqRefIndex);
            //    }
            //}
            //filterStateMitigationReferences(stateName, m_ref, machineName_, clusterName);
        }

        public static void updateStateMitigationReferences(StateMitigationReferenceDB s_db, int mitigationSeqRefIndex)
        {
            //var refs = (from ref_ in db.StateMitigationReferenceDBs
            //            where ref_.clusterName == s_db.clusterName
            //            && ref_.machineName == s_db.machineName
            //            && ref_.stateName == s_db.stateName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        if (s_db.stateName == re.stateName
            //           && s_db.machineName == re.machineName
            //           && s_db.clusterName == re.clusterName
            //           && re.referencedTransitionID == s_db.referencedTransitionID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        if (s_db.stateName == re.stateName
            //           && s_db.machineName == re.machineName
            //           && s_db.clusterName == re.clusterName
            //           && re.referencedStateID == s_db.referencedStateID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        if (s_db.stateName == re.stateName
            //           && s_db.machineName == re.machineName
            //           && s_db.clusterName == re.clusterName
            //           && re.referencedActionID == s_db.referencedActionID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //}
        }

        public static void filterStateMitigationReferences(string stateName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //var refs = (from ref_ in db.StateMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.stateName == stateName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        bool valid = false;
            //        foreach (Transition trans in m_ref.transitions)
            //        {
            //            if (re.referencedTransitionID == trans.transitionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        bool valid = false;
            //        foreach (State state in m_ref.states)
            //        {
            //            if (re.referencedStateID == state.stateID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        bool valid = false;
            //        foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //        {
            //            if (re.referencedActionID == action.actionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.StateMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //}
        }

        public static bool containsNullStateMitigationReferences(string stateName, string machineName_, string clusterName)
        {
            bool containsNull = false;

            //var refs = (from ref_ in db.StateMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.stateName == stateName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        if (re.sequenceIndex != null)
            //        {
            //            containsNull = true;
            //            break;
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        if (re.sequenceIndex != null)
            //        {
            //            containsNull = true;
            //            break;
            //        }
            //    }
            //    //else if (re.referencedActionID != null)
            //    //{
            //    //    if (re.sequenceIndex != null)
            //    //    {
            //    //        containsNull = true;
            //    //        break;
            //    //    }
            //    //}
            //}

            return containsNull;
        }

        public static MitigationReferences loadStateMitigationReferencesDB(string stateName, string machineName_, string clusterName)
        {
            MitigationReferences m_ref = new MitigationReferences();

            //var refs = (from ref_ in db.StateMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.stateName == stateName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        int transID = (int)re.referencedTransitionID;
            //        Transition trans = loadStateTransitionDB(transID, machineName_, clusterName);
            //        if (re.sequenceIndex != null)
            //            trans.mitigationSeqRefIndex = (int)re.sequenceIndex;
            //        m_ref.addTransition(trans);
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        int stateID = (int)re.referencedStateID;
            //        State state = loadStateDB(stateID, clusterName);
            //        if (re.sequenceIndex != null)
            //           state.mitigationSeqRefIndex = (int)re.sequenceIndex;
            //        m_ref.addState(state);
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        int actionID = (int)re.referencedActionID;
            //        PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionID, clusterName);
            //        if (re.sequenceIndex != null) 
            //            action.mitigationSeqRefIndex = (int)re.sequenceIndex; 
            //        m_ref.addAction(action);
            //    }
            //}

            return m_ref;
        }

        public static void addTransitionMitigationReferencesDB(string transitionName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //{
            //    TransitionMitigationReferenceDB s_db = new TransitionMitigationReferenceDB();
            //    s_db.transitionName = transitionName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedActionID = action.actionID;
            //    s_db.sequenceIndex = action.mitigationSeqRefIndex;

            //    TransitionMitigationReferenceDB ref_exist = findTransitionMitigationReferenceAction(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.TransitionMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateTransitionMitigationReferences(s_db, action.mitigationSeqRefIndex);
            //    }
            //}

            //foreach (State state in m_ref.states)
            //{
            //    TransitionMitigationReferenceDB s_db = new TransitionMitigationReferenceDB();
            //    s_db.transitionName = transitionName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedStateID = state.stateID;
            //    s_db.sequenceIndex = state.mitigationSeqRefIndex;

            //    TransitionMitigationReferenceDB ref_exist = findTransitionMitigationReferenceState(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.TransitionMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateTransitionMitigationReferences(s_db, state.mitigationSeqRefIndex);
            //    }
            //}

            //foreach (Transition trans in m_ref.transitions)
            //{
            //    TransitionMitigationReferenceDB s_db = new TransitionMitigationReferenceDB();
            //    s_db.transitionName = transitionName;
            //    s_db.machineName = machineName_;
            //    s_db.clusterName = clusterName;
            //    s_db.referencedTransitionID = trans.transitionID;
            //    s_db.sequenceIndex = trans.mitigationSeqRefIndex;

            //    TransitionMitigationReferenceDB ref_exist = findTransitionMitigationReferenceTransition(s_db);
            //    if (ref_exist == null)
            //    {
            //        db.TransitionMitigationReferenceDBs.InsertOnSubmit(s_db);
            //        db.SubmitChanges();
            //    }
            //    else
            //    {
            //        updateTransitionMitigationReferences(s_db, trans.mitigationSeqRefIndex);
            //    }
            //}

            //filterTransitionMitigationReferences(transitionName, m_ref, machineName_, clusterName);
        }

        public static void updateTransitionMitigationReferences(TransitionMitigationReferenceDB s_db, int mitigationSeqRefIndex)
        {
            //var refs = (from ref_ in db.TransitionMitigationReferenceDBs
            //            where ref_.clusterName == s_db.clusterName
            //            && ref_.machineName == s_db.machineName
            //            && ref_.transitionName == s_db.transitionName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        if (s_db.transitionName == re.transitionName
            //            && s_db.machineName == re.machineName
            //            && s_db.clusterName == re.clusterName
            //            && re.referencedTransitionID == s_db.referencedTransitionID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        if (s_db.transitionName == re.transitionName
            //            && s_db.machineName == re.machineName
            //            && s_db.clusterName == re.clusterName
            //            && re.referencedStateID == s_db.referencedStateID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        if (s_db.transitionName == re.transitionName
            //            && s_db.machineName == re.machineName
            //            && s_db.clusterName == re.clusterName
            //            && re.referencedActionID == s_db.referencedActionID)
            //        {
            //            re.sequenceIndex = mitigationSeqRefIndex;
            //            db.SubmitChanges();
            //        }
            //    }
            //}
        }

        public static void filterTransitionMitigationReferences(string transitionName, MitigationReferences m_ref, string machineName_, string clusterName)
        {
            //var refs = (from ref_ in db.TransitionMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.transitionName == transitionName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        bool valid = false;
            //        foreach (Transition trans in m_ref.transitions)
            //        {
            //            if (re.referencedTransitionID == trans.transitionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        bool valid = false;
            //        foreach (State state in m_ref.states)
            //        {
            //            if (re.referencedStateID == state.stateID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        bool valid = false;
            //        foreach (PMisuseAn.behaviour.model.Action action in m_ref.actions)
            //        {
            //            if (re.referencedActionID == action.actionID)
            //            {
            //                valid = true;
            //            }
            //        }

            //        if (!valid)
            //        {
            //            db.TransitionMitigationReferenceDBs.DeleteOnSubmit(re);
            //            db.SubmitChanges();
            //        }
            //    }
            //}
        }

        public static bool containsNullTransitionMitigationReferences(string transitionName, string machineName_, string clusterName)
        {
            bool containsNull = false;
            //var refs = (from ref_ in db.TransitionMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.transitionName == transitionName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        if (re.sequenceIndex != null)
            //        {
            //            containsNull = true;
            //            break;
            //        }
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        if (re.sequenceIndex != null)
            //        {
            //            containsNull = true;
            //            break;
            //        }
            //    }
            //    //else if (re.referencedActionID != null)
            //    //{
            //    //    if (re.sequenceIndex != null)
            //    //    {
            //    //        containsNull = true;
            //    //        break;
            //    //    }
            //    //}
            //}
            return containsNull;
        }

        public static MitigationReferences loadTransitionMitigationReferencesDB(string transitionName, string machineName_, string clusterName)
        {
            MitigationReferences m_ref = new MitigationReferences();

            //var refs = (from ref_ in db.TransitionMitigationReferenceDBs
            //            where ref_.clusterName == clusterName
            //            && ref_.machineName == machineName_
            //            && ref_.transitionName == transitionName
            //            select ref_);

            //foreach (var re in refs)
            //{
            //    if (re.referencedTransitionID != null)
            //    {
            //        int transID = (int)re.referencedTransitionID;
            //        Transition trans = loadStateTransitionDB(transID, machineName_, clusterName);
            //        if (re.sequenceIndex != null)
            //            trans.mitigationSeqRefIndex = (int)re.sequenceIndex; 
            //        m_ref.addTransition(trans);
            //    }
            //    else if (re.referencedStateID != null)
            //    {
            //        int stateID = (int)re.referencedStateID;
            //        State state = loadStateDB(stateID, clusterName);
            //        if(re.sequenceIndex != null)
            //            state.mitigationSeqRefIndex = (int)re.sequenceIndex;
            //        m_ref.addState(state);
            //    }
            //    else if (re.referencedActionID != null)
            //    {
            //        int actionID = (int)re.referencedActionID;
            //        PMisuseAn.behaviour.model.Action action = loadStateActionDB(actionID, clusterName);
            //        if (re.sequenceIndex != null)
            //            action.mitigationSeqRefIndex = (int)re.sequenceIndex; 
            //        m_ref.addAction(action);
            //    }
            //}

            return m_ref;
        }


        public static GroupDB findGroup(string groupName, string clusterName)
        {
            GroupDB gpDB = GroupSerialisationEngine.deSerialiseGroupDB(groupName, clusterName);
            return gpDB;

            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           && gp.groupName == groupName
            //           select gp).SingleOrDefault<GroupDB>();

            //if (gps == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return gps;
            //}
        }

        public static void addGroupDB(Group group, string clusterName)
        {
            GroupDB gdb = new GroupDB(group);
            GroupSerialisationEngine.serialise(group.groupName, clusterName, gdb);

            //GroupDB g_db = new GroupDB();
            //g_db.clusterName = clusterName;
            //g_db.groupName = group.groupName;

            //if (group.groupType == Group.GroupType.Planned)
            //{
            //    g_db.groupType = "Planned";
            //}
            //else
            //{
            //    g_db.groupType = "Emergent";
            //}

            //GroupDB group_exist = findGroup(group.groupName, clusterName);
            //if (group_exist == null)
            //{
            //    db.GroupDBs.InsertOnSubmit(g_db);
            //    db.SubmitChanges();
            //}
            //else
            //{
            //    updateGroupDB(group, clusterName);
            //}

            //foreach (Agent agent in group.members)
            //{
            //    addGroupAgentDB(agent, group.groupName, clusterName);
            //}
        }

        private static void updateGroupDB(Group group_, string clusterName)
        {
            GroupSerialisationEngine.deleteGroupDB(group_.groupName, clusterName);

            GroupDB gdb = new GroupDB(group_);
            GroupSerialisationEngine.serialise(group_.groupName, clusterName, gdb);


            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           && gp.groupName == group_.groupName
            //           select gp).SingleOrDefault<GroupDB>();

            //if (gps != null)
            //{
            //    if (group_.groupType == Group.GroupType.Planned)
            //    {
            //        gps.groupType = "Planned";
            //    }
            //    else
            //    {
            //        gps.groupType = "Emergent";
            //    }
            //    db.SubmitChanges();
            //}
        }

        public static void deleteGroupDB(string groupName, string clusterName)
        {
            GroupSerialisationEngine.deleteGroupDB(groupName, clusterName);

            //deleteGroupAgentDBGroup(groupName, clusterName);
            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           && gp.groupName == groupName
            //           select gp).SingleOrDefault<GroupDB>();

            //if (gps != null)
            //{
            //    db.GroupDBs.DeleteOnSubmit(gps);
            //    db.SubmitChanges();
            //}
        }

        private static void deleteGroupDBCluster(string clusterName)
        {
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            foreach (string gn in groupNames)
            {
                GroupSerialisationEngine.deleteGroupDB(gn, clusterName);
            }

            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           select gp);

            //if (gps != null)
            //{
            //    foreach (GroupDB g in gps)
            //    {
            //        db.GroupDBs.DeleteOnSubmit(g);
            //        db.SubmitChanges();
            //    }                
            //}
        }

        public static List<string> loadAllGroupNamesDB(string clusterName)
        {
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            return groupNames;

            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           select gp);

            //List<string> groupNames = new List<string>();

            //foreach (GroupDB g in gps)
            //{
            //    string groupName = g.groupName;
            //    groupNames.Add(groupName);
            //}

            //return groupNames;

        }

        public static Group loadGroupDB(string groupName, string clusterName)
        {
            Group group = null;
            GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
            if (gdb != null)
            {
                group = gdb.group;
            }
            return group;

            //Group group = null;
            //var gps = (from gp in db.GroupDBs
            //           where gp.clusterName == clusterName
            //           && gp.groupName == groupName
            //           select gp).SingleOrDefault<GroupDB>();

            //if (gps != null)
            //{
            //    group = new Group();
            //    group.groupName = groupName;
            //    group.clusterName = clusterName;
            //    group.groupID = gps.groupID;

            //    if (gps.groupType == "Planned")
            //    {
            //        group.groupType = Group.GroupType.Planned;
            //    }
            //    else
            //    {
            //        group.groupType = Group.GroupType.Emergent;
            //    }
            //    List<Agent> groupAgents = loadGroupAgentDB(groupName, clusterName);
            //    foreach (Agent agent in groupAgents)
            //    {
            //        List<string> agentAssociatedGroups = loadAgentAssociatedGroups(agent.agentName, clusterName);
            //        agent.associatedGroups = agentAssociatedGroups;
            //    }
            //    group.members = groupAgents;
            //}

            //return group;
        }

        public static List<string> loadAgentAssociatedGroups(string agentName, string clusterName)
        {
            List<string> associatedGroups = new List<string>();
            List<string> groups = GroupSerialisationEngine.getGroupsList(clusterName);

            foreach (string groupName in groups)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
                foreach (Agent agent in gdb.group.members)
                {
                    if (agent.agentName.Equals(agentName))
                    {
                        associatedGroups.Add(groupName);
                        break;
                    }
                }
            }

            return associatedGroups;

            //List<string> associatedGroups = new List<string>();
            //var gps = (from gp in db.GroupAgentDBs
            //           where gp.clusterName == clusterName
            //           && gp.agentName == agentName
            //           select gp);
            //foreach (GroupAgentDB ga_db in gps)
            //{
            //    associatedGroups.Add(ga_db.groupName);
            //}

            //return associatedGroups;
        }

        //public static GroupAgentDB findGroupAgent(string agentName, string groupName, string clusterName)
        //{
        //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //    if (gadb == null)
        //        return null; 

        //    bool exist = false;
        //    foreach(string assGp in gadb.agent.associatedGroups)
        //    {
        //        if (assGp.Equals(groupName))
        //        {
        //            exist = true;
        //            break;
        //        }
        //    }
        //    if (exist == true)
        //    {
        //        return gadb;
        //    }
        //    else
        //    {
        //        return null;
        //    }

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           && gp.agentName == agentName
        //    //           select gp).SingleOrDefault<GroupAgentDB>();

        //    //if (gps == null)
        //    //{
        //    //    return null;
        //    //}
        //    //else
        //    //{
        //    //    return gps;
        //    //}
        //}

        //public static List<Agent> loadGroupAgentDB(string groupName, string clusterName)
        //{
        //    List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
        //    List<Agent> groupAgents = new List<Agent>();
        //    foreach (string agentName in agentNames)
        //    {
        //        GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //        foreach (string assGp in gadb.agent.associatedGroups)
        //        {
        //            if (assGp.Equals(groupName))
        //            {
        //                groupAgents.Add(gadb.agent);
        //            }
        //        }
        //    }
        //    return groupAgents;

        //    //List<Agent> groupAgents = new List<Agent>();
        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           select gp);

        //    //foreach (GroupAgentDB ga_db in gps)
        //    //{
        //    //    Agent agent = new Agent();
        //    //    agent.agentID = ga_db.agentID;
        //    //    agent.agentName = ga_db.agentName;
        //    //    agent.clusterName = clusterName;

        //    //    Actor actorType = loadActor(ga_db.actorType, clusterName);
        //    //    agent.actorType = actorType;

        //    //    StateMachine assignedMachine = loadStateMachineDB(ga_db.machineName, clusterName);
        //    //    agent.assignedMachine = assignedMachine;

        //    //    groupAgents.Add(agent);
        //    //}

        //    //return groupAgents;
        //}

        //public static Agent loadGroupAgentDB(string agentName, string groupName, string clusterName)
        //{
        //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //    if (gadb == null)
        //        return null;

        //    Agent agent = null;
        //    foreach (string assGp in gadb.agent.associatedGroups)
        //    {
        //        if (assGp.Equals(groupName))
        //        {
        //            agent = gadb.agent;
        //        }
        //    }
        //    return agent;

        //    //Agent agent = null;
        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           && gp.agentName == agentName
        //    //           select gp).SingleOrDefault<GroupAgentDB>();

        //    //if (gps != null)
        //    //{
        //    //    agent = new Agent();
        //    //    agent.agentID = gps.agentID;
        //    //    agent.agentName = gps.agentName;
        //    //    agent.clusterName = clusterName;

        //    //    Actor actorType = loadActor(gps.actorType, clusterName);
        //    //    agent.actorType = actorType;

        //    //    StateMachine assignedMachine = loadStateMachineDB(gps.machineName, clusterName);
        //    //    agent.assignedMachine = assignedMachine;

        //    //    List<string> agentAssociatedGroups = loadAgentAssociatedGroups(agent.agentName, clusterName);
        //    //    agent.associatedGroups = agentAssociatedGroups;
        //    //}
        //    //return agent;
        //}

        //public static Agent loadGroupAgentDBAgent(string agentName, string clusterName)
        //{
        //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //    if (gadb == null)
        //        return null;

        //    return gadb.agent;

        //    //Agent agent = null;
        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.agentName == agentName
        //    //           select gp);

        //    //if (gps != null)
        //    //{
        //    //    foreach (var gp in gps)
        //    //    {
        //    //        agent = new Agent();
        //    //        agent.agentID = gp.agentID;
        //    //        agent.agentName = gp.agentName;
        //    //        agent.clusterName = clusterName;

        //    //        Actor actorType = loadActor(gp.actorType, clusterName);
        //    //        agent.actorType = actorType;

        //    //        StateMachine assignedMachine = loadStateMachineDB(gp.machineName, clusterName);
        //    //        agent.assignedMachine = assignedMachine;

        //    //        List<string> agentAssociatedGroups = loadAgentAssociatedGroups(agent.agentName, clusterName);
        //    //        agent.associatedGroups = agentAssociatedGroups;

        //    //        break; //iterate only once since its the same agent machine that is associated with different groups
        //    //    }

        //    //}
        //    //return agent;
        //}

        private static void setAssociatedGroups(Agent agent, string clusterName)
        {
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            foreach (string groupName in groupNames)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
                foreach (Agent a in gdb.group.members)
                {
                    if (a.agentName.Equals(agent.agentName))
                    {
                        agent.addAssociatedGroup(groupName);
                        break;
                    }
                }
            }
        }

        public static List<Agent> loadGroupAgentDBMachine(string machineName, string clusterName)
        {
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            List<Agent> agents = new List<Agent>();
            foreach (string groupName in groupNames)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
                foreach (Agent agent in gdb.group.members)
                {
                    if (agent.assignedMachine.machineName.Equals(machineName))
                    {
                        bool exist = false;
                        foreach (Agent an in agents)
                        {
                            if (an.agentName.Equals(agent.agentName))
                            {
                                exist = true;
                                break;
                            }
                        }
                        if (exist == false)
                        {
                            setAssociatedGroups(agent, clusterName);
                            agents.Add(agent);
                        }
                    }
                }
            }

            return agents;

            //Agent agent = null;
            //var gps = (from gp in db.GroupAgentDBs
            //           where gp.clusterName == clusterName
            //           && gp.machineName == machineName
            //           select gp);

            //List<Agent> mac_agents = new List<Agent>();
            //if (gps != null)
            //{
            //    foreach (var adb in gps)
            //    {
            //        agent = new Agent();
            //        agent.agentID = adb.agentID;
            //        agent.agentName = adb.agentName;
            //        agent.clusterName = clusterName;

            //        Actor actorType = loadActor(adb.actorType, clusterName);
            //        agent.actorType = actorType;

            //        StateMachine assignedMachine = loadStateMachineDB(adb.machineName, clusterName);
            //        agent.assignedMachine = assignedMachine;

            //        List<string> agentAssociatedGroups = loadAgentAssociatedGroups(agent.agentName, clusterName);
            //        agent.associatedGroups = agentAssociatedGroups;
            //        mac_agents.Add(agent);
            //    }                
            //}
            //return mac_agents;
        }


        public static List<string> loadAllAgentNames(string clusterName)
        {
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            List<string> agentNames = new List<string>();
            foreach (string groupName in groupNames)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
                foreach (Agent agent in gdb.group.members)
                {
                    bool exist = false;
                    foreach (string an in agentNames)
                    {
                        if (an.Equals(agent.agentName))
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (exist == false)
                    {
                        agentNames.Add(agent.agentName);
                    }
                }
            }

            //List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
            return agentNames;

            //List<string> allAgentNames = new List<string>();
            //var gps = (from gp in db.GroupAgentDBs
            //           where gp.clusterName == clusterName
            //           select gp);

            //if (gps != null)
            //{
            //    foreach (var g in gps)
            //    {
            //        bool contained = false;
            //        foreach (string gx in allAgentNames)
            //        {
            //            if (gx.Equals(g.agentName))
            //            {
            //                contained = true;
            //                break;
            //            }
            //        }
            //        if (!contained)
            //        {
            //            allAgentNames.Add(g.agentName);
            //        }
            //    }
            //}

            //return allAgentNames;
        }


        public static Agent loadAgentDB(string agentName, string clusterName)
        {
            Agent agent = null;
            List<string> groupNames = GroupSerialisationEngine.getGroupsList(clusterName);
            foreach (string groupName in groupNames)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
                foreach (Agent a in gdb.group.members)
                {
                    if (a.agentName.Equals(agentName))
                    {
                        if (agent == null)
                        {
                            agent = a;
                        }
                        agent.addAssociatedGroup(groupName);
                        break;
                    }
                }
            }
            return agent;
        }

        public static void deleteGroupAgentDB(string agentName, string groupName, string clusterName)
        {
            GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
            GroupSerialisationEngine.deleteGroupDB(groupName, clusterName);
            foreach (Agent a in gdb.group.members)
            {
                if (a.agentName.Equals(agentName))
                {
                    gdb.group.removeAgent(a);
                    break;
                }
            }
            GroupSerialisationEngine.serialise(groupName, clusterName, gdb);
        }

        public static Agent loadGroupAgentDB(string agentName, string groupName, string clusterName)
        {
            Agent agent = null;
            GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, groupName);
            foreach (Agent a in gdb.group.members)
            {
                if (a.agentName.Equals(agentName))
                {
                    if (agent == null)
                    {
                        agent = a;
                    }
                    setAssociatedGroups(agent, clusterName);
                    break;
                }
            }

            return agent;
        }

        public static List<string> loadAgentsInGroupDB(List<string> groupList, string clusterName)
        {
            List<string> allAgentNamesInGroup = new List<string>();
            foreach (string gr in groupList)
            {
                GroupDB gdb = GroupSerialisationEngine.deSerialiseGroupDB(clusterName, gr);
                foreach (Agent agent in gdb.group.members)
                {
                    bool exist = false;
                    foreach (string an in allAgentNamesInGroup)
                    {
                        if (an.Equals(agent.agentName))
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (exist == false)
                    {
                        allAgentNamesInGroup.Add(agent.agentName);
                    }
                }

                //List<string> agents = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
                //foreach (string agentName in agents)
                //{
                //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
                //    foreach (string assGroup in gadb.agent.associatedGroups)
                //    {
                //        if (assGroup.Equals(gr))
                //        {
                //            bool contained = false;
                //            foreach (string gx in allAgentNamesInGroup)
                //            {
                //                if (gx.Equals(agentName))
                //                {
                //                    contained = true;
                //                    break;
                //                }
                //            }
                //            if (!contained)
                //            {
                //                allAgentNamesInGroup.Add(gadb.agent.agentName);
                //            }
                //        }
                //    }
                //}
            }

            return allAgentNamesInGroup;

            //List<string> allAgentNamesInGroup = new List<string>();
            //foreach (string gr in groupList)
            //{
            //    var gps = (from gp in db.GroupAgentDBs
            //               where gp.clusterName == clusterName
            //               && gp.groupName == gr
            //               select gp);

            //    if (gps != null)
            //    {
            //        foreach (var g in gps)
            //        {
            //            bool contained = false;
            //            foreach (string gx in allAgentNamesInGroup)
            //            {
            //                if (gx.Equals(g.agentName))
            //                {
            //                    contained = true;
            //                    break;
            //                }
            //            }
            //            if (!contained)
            //            {
            //                allAgentNamesInGroup.Add(g.agentName);
            //            }
            //        }
            //    }
            //}

            //return allAgentNamesInGroup;
        }

        //public static void addGroupAgentDB(Agent agent, string groupName, string clusterName)
        //{
        //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agent.agentName);

        //    if (gadb == null)
        //    {
        //        agent.addAssociatedGroup(groupName);
        //        gadb = new GroupAgentDB(agent);
        //        GroupAgentSerialisationAgent.serialise(agent.agentName, clusterName, gadb);
        //    }
        //    else
        //    {
        //        updateGroupAgentDB(agent, groupName, clusterName);
        //    }

        //    //GroupAgentDB ga_db = new GroupAgentDB();
        //    //ga_db.agentName = agent.agentName;
        //    //ga_db.groupName = groupName;
        //    //ga_db.clusterName = clusterName;
        //    //ga_db.machineName = agent.assignedMachine.machineName;
        //    //ga_db.actorType = agent.actorType.name;

        //    //GroupAgentDB agent_exist = findGroupAgent(agent.agentName, groupName, clusterName);
        //    //if (agent_exist == null)
        //    //{
        //    //    db.GroupAgentDBs.InsertOnSubmit(ga_db);
        //    //    db.SubmitChanges();
        //    //}
        //    //else
        //    //{
        //    //    updateGroupAgentDB(agent, groupName, clusterName);
        //    //}
        //}

        //private static void updateGroupAgentDB(Agent agent, string groupName, string clusterName)
        //{
        //    GroupAgentSerialisationAgent.deleteGroupAgentDB(agent.agentName, clusterName);

        //    agent.addAssociatedGroup(groupName);
        //    GroupAgentDB gadb = new GroupAgentDB(agent);
        //    GroupAgentSerialisationAgent.serialise(agent.agentName, clusterName, gadb);

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           && gp.agentName == agent.agentName
        //    //           select gp).SingleOrDefault<GroupAgentDB>();

        //    //if (gps != null)
        //    //{
        //    //    gps.machineName = agent.assignedMachine.machineName;
        //    //    gps.actorType = agent.actorType.name;

        //    //    db.SubmitChanges();
        //    //}
        //}

        //public static void deleteGroupAgentDB(string agentName, string groupName, string clusterName)
        //{
        //    GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //    GroupAgentSerialisationAgent.deleteGroupAgentDB(agentName, clusterName);
        //    gadb.agent.removeAssociatedGroup(groupName);
        //    GroupAgentSerialisationAgent.serialise(agentName, clusterName, gadb);

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           && gp.agentName == agentName
        //    //           select gp).SingleOrDefault<GroupAgentDB>();

        //    //if (gps != null)
        //    //{
        //    //    db.GroupAgentDBs.DeleteOnSubmit(gps);
        //    //    db.SubmitChanges();
        //    //}
        //}

        //private static void deleteGroupAgentDBGroup(string groupName, string clusterName)
        //{
        //    List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
        //    foreach (string agentName in agentNames)
        //    {
        //        GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //        GroupAgentSerialisationAgent.deleteGroupAgentDB(agentName, clusterName);
        //        foreach (string assgp in gadb.agent.associatedGroups)
        //        {
        //            if (assgp.Equals(groupName))
        //            {
        //                gadb.agent.removeAssociatedGroup(groupName);
        //                GroupAgentSerialisationAgent.serialise(agentName, clusterName, gadb);
        //                break;
        //            }
        //        }
        //    }

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.groupName == groupName
        //    //           select gp);

        //    //if (gps != null)
        //    //{
        //    //    foreach (GroupAgentDB gdb in gps)
        //    //    {
        //    //        db.GroupAgentDBs.DeleteOnSubmit(gdb);
        //    //        db.SubmitChanges();
        //    //    }
        //    //}
        //}

        //private static void deleteGroupAgentDBMachine(string machineName, string clusterName)
        //{
        //    List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
        //    foreach (string agentName in agentNames)
        //    {
        //        GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //        if (gadb.agent.assignedMachine.machineName.Equals(machineName))
        //        {
        //            GroupAgentSerialisationAgent.deleteGroupAgentDB(agentName, clusterName);
        //        }
        //    }

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.machineName == machineName
        //    //           select gp);

        //    //if (gps != null)
        //    //{
        //    //    foreach (GroupAgentDB gdb in gps)
        //    //    {
        //    //        db.GroupAgentDBs.DeleteOnSubmit(gdb);
        //    //        db.SubmitChanges();
        //    //    }
        //    //}
        //}

        //private static void deleteGroupAgentDBActorType(string actorType, string clusterName)
        //{
        //    List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
        //    foreach (string agentName in agentNames)
        //    {
        //        GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //        if (gadb.agent.actorType.Equals(actorType))
        //        {
        //            GroupAgentSerialisationAgent.deleteGroupAgentDB(agentName, clusterName);
        //        }
        //    }

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           && gp.actorType == actorType
        //    //           select gp);

        //    //if (gps != null)
        //    //{
        //    //    foreach (GroupAgentDB gdb in gps)
        //    //    {
        //    //        db.GroupAgentDBs.DeleteOnSubmit(gdb);
        //    //        db.SubmitChanges();
        //    //    }
        //    //}
        //}

        //private static void deleteGroupAgentDBCluster(string clusterName)
        //{
        //    List<string> agentNames = GroupAgentSerialisationAgent.getGroupAgentsList(clusterName);
        //    foreach (string agentName in agentNames)
        //    {
        //        GroupAgentDB gadb = GroupAgentSerialisationAgent.deSerialiseGroupAgentDB(clusterName, agentName);
        //        GroupAgentSerialisationAgent.deleteGroupAgentDB(agentName, clusterName);
        //    }

        //    //var gps = (from gp in db.GroupAgentDBs
        //    //           where gp.clusterName == clusterName
        //    //           select gp);

        //    //if (gps != null)
        //    //{
        //    //    foreach (GroupAgentDB gdb in gps)
        //    //    {
        //    //        db.GroupAgentDBs.DeleteOnSubmit(gdb);
        //    //        db.SubmitChanges();
        //    //    }
        //    //}
        //}

        //public static void addAgentCall(DateTime time, string agentFromName,
        //    string agentToName, int callFromStateID, int callToStateID,
        //    int callFromTransitionID, int callToTransitionID, string callFromGroupName,
        //    string callToGroupName, string clusterName)
        //{
        //    GroupAgentCallDB ga_db = new GroupAgentCallDB();
        //    ga_db.callTime = time;
        //    ga_db.clusterName = clusterName;
        //    ga_db.agentFromName = agentFromName;
        //    ga_db.agentToName = agentToName;
        //    ga_db.callFromStateID = callFromStateID;
        //    ga_db.callToStateID = callToStateID;
        //    ga_db.callFromTransitionID = callFromTransitionID;
        //    ga_db.callToTransitionID = callToTransitionID;
        //    ga_db.callFromGroupName = callFromGroupName;
        //    ga_db.callToGroupName = callToGroupName;

        //    db.GroupAgentCallDBs.InsertOnSubmit(ga_db);
        //    db.SubmitChanges();

        //}

        //public static void deleteAgentCallDB(string agentName, string groupName, string clusterName)
        //{
        //    var gps = (from gp in db.GroupAgentCallDBs
        //               where gp.clusterName == clusterName
        //               && (gp.callFromGroupName == groupName || gp.callToGroupName == groupName)
        //               && (gp.agentFromName == agentName || gp.agentToName == agentName)
        //               select gp);

        //    if (gps != null)
        //    {
        //        foreach (GroupAgentCallDB gdb in gps)
        //        {
        //            db.GroupAgentCallDBs.DeleteOnSubmit(gdb);
        //            db.SubmitChanges();
        //        }
        //    }
        //}

        //public static void deleteAgentCallDBGroup(string groupName, string clusterName)
        //{
        //    var gps = (from gp in db.GroupAgentCallDBs
        //               where gp.clusterName == clusterName
        //               && (gp.callFromGroupName == groupName || gp.callToGroupName == groupName)
        //               select gp);

        //    if (gps != null)
        //    {
        //        foreach (GroupAgentCallDB gdb in gps)
        //        {
        //            db.GroupAgentCallDBs.DeleteOnSubmit(gdb);
        //            db.SubmitChanges();
        //        }
        //    }
        //}

        //public List<AgentCall> loadGroupAgentCallsDB(string agentName, string groupName, string clusterName)
        //{
        //    List<AgentCall> agentCalls = new List<AgentCall>();

        //     var gps = (from gp in db.GroupAgentCallDBs
        //               where gp.clusterName == clusterName
        //               && (gp.callFromGroupName == groupName || gp.callToGroupName == groupName)
        //               && (gp.agentFromName == agentName || gp.agentToName == agentName)
        //               select gp);

        //     if (gps != null)
        //     {
        //         foreach (GroupAgentCallDB gdb in gps)
        //         {
        //             AgentCall ac = new AgentCall();
        //             ac.callTime = gdb.callTime;
        //             ac.clusterName = gdb.clusterName;
        //             ac.agentFromName = gdb.agentFromName;
        //             ac.agentToName = gdb.agentToName;
        //             ac.callToStateID = gdb.callToStateID;
        //             ac.callFromStateID = gdb.callFromStateID;
        //             ac.callFromTransitionID = gdb.callFromTransitionID;
        //             ac.callToTransitionID = gdb.callToTransitionID;
        //             ac.callFromGroupName = gdb.callFromGroupName;
        //             ac.callToGroupName = gdb.callToGroupName;

        //             agentCalls.Add(ac);
        //         }
        //     }

        //     return agentCalls;
        //}

        public static ThreatAdaptationActionDescDB getAdaptationDescription(int adaptationSeqNo, string subjectAgentName)
        {
            ThreatAdaptationActionDescDB aa_db = ThreatAdaptationActionDescSerialisationEngine.deSerialiseAdaptationHistory(adaptationSeqNo, subjectAgentName);
            return aa_db;
        }

        private static int adaptationSeqNo = 1;

        public static void writeAdaptationDescription(behaviour.adaptation.AdaptationDescriptor ad,
            string violatingPolicyStatement, string threatMessageTransition, string violatingContext, string rationale, int seqTimeline, string subjectAgentName)
        {
            ThreatAdaptationActionDescDB aa_db = new ThreatAdaptationActionDescDB(ad, violatingPolicyStatement, threatMessageTransition, violatingContext, rationale, seqTimeline);
            string seqno = "" + adaptationSeqNo;
            ThreatAdaptationActionDescSerialisationEngine.serialise(seqno, subjectAgentName, aa_db);
            adaptationSeqNo = adaptationSeqNo + 1;        

        }

        public static AdaptationDescriptorBinding getAdaptationProperties(behaviour.adaptation.AdaptationDescriptor ad,
            string violatingPolicyStatement, string threatMessageTransition, string violatingContext, string rationale, int seqTimeline, string subjectAgentName)
        {
            AdaptationDescriptorBinding aa_db = new AdaptationDescriptorBinding();
            aa_db.seqTimeline = ""+seqTimeline;
            //aa_db.threatMessageTransition = threatMessageTransition;
            //aa_db.violatingPolicyStatement = violatingPolicyStatement;
            //aa_db.violatingContext = violatingContext;
            //aa_db.rationale = rationale;

            string attSensLevel = "";
            int j = 0;
            int count_sl = 1;
            foreach (Attribute att in ad.threathenedOperationalAttributes)
            {
                attSensLevel = attSensLevel + att.name + "=" + att.sensitivityLevel;
                if (j < ad.threathenedOperationalAttributes.Count() - 1)
                {
                    attSensLevel = attSensLevel + ", ";
                }
                j = j + 1;

                if (count_sl == 2)
                {
                    attSensLevel = attSensLevel + "\r";
                    count_sl = 1;
                }
                count_sl = count_sl + 1;
            }
            aa_db.attSensLevel = attSensLevel;
            aa_db.SLConstant = "" + ad.sensitivity_level_constant;

            string sensitivityLevelUtilitys = "";
            j = 0;
            int countslul = 1;
            foreach (double dd in ad.sensitivity_level_utilitys)
            {
                sensitivityLevelUtilitys = sensitivityLevelUtilitys + dd;

                if (j < ad.sensitivity_level_utilitys.Count() - 1)
                {
                    sensitivityLevelUtilitys = sensitivityLevelUtilitys + ", ";
                }
                j = j + 1;

                if (countslul == 5)
                {
                    sensitivityLevelUtilitys = sensitivityLevelUtilitys + "\r";
                    countslul = 1;
                }
                countslul = countslul + 1;
            }
            aa_db.SLUtilities = sensitivityLevelUtilitys;

            string attObfsLevel = "";
            j = 0;
            int countol = 1;
            foreach (Attribute att in ad.threathenedOperationalAttributes)
            {
                attObfsLevel = attObfsLevel + att.name + "=" + att.obfuscationLevel;
                if (j < ad.threathenedOperationalAttributes.Count() - 1)
                {
                    attObfsLevel = attObfsLevel + ", ";
                }
                j = j + 1;

                if (countol == 2)
                {
                    attObfsLevel = attObfsLevel + "\r";
                    countol = 1;
                }
                countol = countol + 1;
            }

            aa_db.attObfsLevel = attObfsLevel;
            aa_db.OLConstant = "" + ad.obf_level_constant;

            string obfuscationLevelUtilitys = "";
            j = 0;
            int countolu = 1;
            foreach (double dd in ad.obf_level_utilitys)
            {
                obfuscationLevelUtilitys = obfuscationLevelUtilitys + dd;
                if (j < ad.obf_level_utilitys.Count() - 1)
                {
                    obfuscationLevelUtilitys = obfuscationLevelUtilitys + ", ";
                }
                j = j + 1;

                if (countolu == 5)
                {
                    obfuscationLevelUtilitys = obfuscationLevelUtilitys + "\r";
                    countolu = 1;
                }
                countolu = countolu + 1;
            }
            aa_db.OLUtilities = obfuscationLevelUtilitys;

            string individualDecisions = "";
            j = 0;
            int countid = 1;
            foreach (string st in ad.decisions)
            {
                individualDecisions = individualDecisions + st;
                if (j < ad.decisions.Count() - 1)
                {
                    individualDecisions = individualDecisions + ", ";
                }
                j = j + 1;

                if (countid == 4)
                {
                    individualDecisions = individualDecisions + "\r";
                    countid = 1;
                }
                countid = countid + 1;
            }
            aa_db.attAdaptationAction = individualDecisions;


            string individualExpectedUtility = "";
            j = 0;
            int countidu = 1;
            foreach (double st in ad.maxUtilityFunctions)
            {
                individualExpectedUtility = individualExpectedUtility + st;
                if (j < ad.maxUtilityFunctions.Count() - 1)
                {
                    individualExpectedUtility = individualExpectedUtility + ", ";
                }
                j = j + 1;

                if (countidu == 5)
                {
                    individualExpectedUtility = individualExpectedUtility + "\r";
                    countidu = 1;
                }

                countidu = countidu + 1;
            }
            aa_db.attExpectedUtility = individualExpectedUtility;

            aa_db.adaptationRule = ad.adaptationRule;
            aa_db.metaAdaptationRule = ad.metaAdaptationRule;

            string tfreq = "";
            j = 0;
            int counttf = 1;
            foreach (double tf in ad.threatFrequencys)
            {
                tfreq = tfreq + tf;
                if (j < ad.threatFrequencys.Count() - 1)
                {
                    tfreq = tfreq + ", ";
                }
                j = j + 1;

                if (counttf == 2)
                {
                    tfreq = tfreq + "\r";
                    counttf = 0;
                }

                counttf = counttf + 1;

            }
            aa_db.threatFrequencies = tfreq;

            string adaptationActions = "";
            adaptationActions = adaptationActions + "(";
            int countaa = 1;
            foreach (List<string> h_act in ad.h_adaptationActionss)
            {
                j = 0;
                adaptationActions = adaptationActions + "[";
                foreach (string ha in h_act)
                {
                    adaptationActions = adaptationActions + ha;
                    if (j < h_act.Count() - 1)
                    {
                        adaptationActions = adaptationActions + ", ";
                    }
                    j = j + 1;

                    if (countaa == 4)
                    {
                        adaptationActions = adaptationActions + "\r";
                        countaa = 1;
                    }

                    countaa = countaa + 1;
                }
                adaptationActions = adaptationActions + "]";
            }
            adaptationActions = adaptationActions + ")";

            aa_db.historyAdaptationActions = adaptationActions;

            aa_db.aggregateAdaptationAction = ad.adaptationAction;

            return aa_db;
        }

        public static AdaptationDescriptorBinding getAdaptationProperties(ThreatAdaptationActionDescDB taa_db)
        {
            if (taa_db == null)
                return null;

            AdaptationDescriptor ad = taa_db.ad;
            string violatingPolicyStatement = taa_db.violatingPolicyStatement;
            string threatMessageTransition = taa_db.threatMessageTransition;
            string violatingContext = taa_db.violatingContext;
            string rationale = taa_db.rationale;
            int seqTimeline = taa_db.seqTimeline;
            
            
            AdaptationDescriptorBinding aa_db = new AdaptationDescriptorBinding();
            aa_db.seqTimeline = "" + seqTimeline;
            //aa_db.threatMessageTransition = threatMessageTransition;
            //aa_db.violatingPolicyStatement = violatingPolicyStatement;
            //aa_db.violatingContext = violatingContext;
            //aa_db.rationale = rationale;

            string attSensLevel = "";
            int j = 0;
            int countsl = 1;
            foreach (Attribute att in ad.threathenedOperationalAttributes)
            {
                attSensLevel = attSensLevel + att.name + "=" + att.sensitivityLevel;
                if (j < ad.threathenedOperationalAttributes.Count() - 1)
                {
                    attSensLevel = attSensLevel + ", ";
                }
                j = j + 1;

                if (countsl == 2)
                {
                    attSensLevel = attSensLevel + "\r";
                    countsl = 1;
                }

                countsl = countsl + 1;
            }
            aa_db.attSensLevel = attSensLevel;
            aa_db.SLConstant = "" + ad.sensitivity_level_constant;

            string sensitivityLevelUtilitys = "";
            j = 0;
            int countslu = 1;
            foreach (double dd in ad.sensitivity_level_utilitys)
            {
                sensitivityLevelUtilitys = sensitivityLevelUtilitys + dd;

                if (j < ad.sensitivity_level_utilitys.Count() - 1)
                {
                    sensitivityLevelUtilitys = sensitivityLevelUtilitys + ", ";
                }
                j = j + 1;

                if (countsl == 5)
                {
                    sensitivityLevelUtilitys = sensitivityLevelUtilitys + "\r";
                    countslu = 1;
                }

                countslu = countslu + 1;
            }
            aa_db.SLUtilities = sensitivityLevelUtilitys;

            string attObfsLevel = "";
            j = 0;
            int countol = 1;
            foreach (Attribute att in ad.threathenedOperationalAttributes)
            {
                attObfsLevel = attObfsLevel + att.name + "=" + att.obfuscationLevel;
                if (j < ad.threathenedOperationalAttributes.Count() - 1)
                {
                    attObfsLevel = attObfsLevel + ", ";
                }
                j = j + 1;

                if (countol == 2)
                {
                    attObfsLevel = attObfsLevel + "\r";
                    countol = 1;
                }

                countol = countol + 1;
            }

            aa_db.attObfsLevel = attObfsLevel;
            aa_db.OLConstant = "" + ad.obf_level_constant;

            string obfuscationLevelUtilitys = "";
            j = 0;
            int countolu = 1;
            foreach (double dd in ad.obf_level_utilitys)
            {
                obfuscationLevelUtilitys = obfuscationLevelUtilitys + dd;
                if (j < ad.obf_level_utilitys.Count() - 1)
                {
                    obfuscationLevelUtilitys = obfuscationLevelUtilitys + ", ";
                }
                j = j + 1;

                if (countolu == 5)
                {
                    obfuscationLevelUtilitys = obfuscationLevelUtilitys + "\r";
                    countolu = 1;
                }

                countolu = countolu + 1;
            }
            aa_db.OLUtilities = obfuscationLevelUtilitys;

            string individualDecisions = "";
            j = 0;
            int countd = 1;
            foreach (string st in ad.decisions)
            {
                individualDecisions = individualDecisions + st;
                if (j < ad.decisions.Count() - 1)
                {
                    individualDecisions = individualDecisions + ", ";
                }
                j = j + 1;

                if (countd == 4)
                {
                    individualDecisions = individualDecisions + "\r";
                    countd = 1;
                }

                countd = countd + 1;
            }
            aa_db.attAdaptationAction = individualDecisions;


            string individualExpectedUtility = "";
            j = 0;
            int countidu = 1;
            foreach (double st in ad.maxUtilityFunctions)
            {
                individualExpectedUtility = individualExpectedUtility + st;
                if (j < ad.maxUtilityFunctions.Count() - 1)
                {
                    individualExpectedUtility = individualExpectedUtility + ", ";
                }
                j = j + 1;

                if (countidu == 4)
                {
                    individualExpectedUtility = individualExpectedUtility + "\r";
                    countidu = 1;
                }

                countidu = countidu + 1;
            }
            aa_db.attExpectedUtility = individualExpectedUtility;

            aa_db.adaptationRule = ad.adaptationRule;
            aa_db.metaAdaptationRule = ad.metaAdaptationRule;

            string tfreq = "";
            j = 0;
            int counttf = 1;
            foreach (double tf in ad.threatFrequencys)
            {
                tfreq = tfreq + tf;
                if (j < ad.threatFrequencys.Count() - 1)
                {
                    tfreq = tfreq + ", ";
                }
                j = j + 1;

                if (counttf == 2)
                {
                    tfreq = tfreq + "\r";
                    counttf = 0;
                }

                counttf = counttf + 1;
            }
            aa_db.threatFrequencies = tfreq;

            string adaptationActions = "";
            adaptationActions = adaptationActions + "(";
            foreach (List<string> h_act in ad.h_adaptationActionss)
            {
                j = 0;
                int countaa = 1;
                adaptationActions = adaptationActions + "[";
                foreach (string ha in h_act)
                {
                    adaptationActions = adaptationActions + ha;
                    if (j < h_act.Count() - 1)
                    {
                        adaptationActions = adaptationActions + ", ";
                    }
                    j = j + 1;

                    if (countaa == 4)
                    {
                        adaptationActions = adaptationActions + "\r";
                        countaa = 1;
                    }
                    countaa = countaa + 1;
                }
                adaptationActions = adaptationActions + "]";
            }
            adaptationActions = adaptationActions + ")";

            aa_db.historyAdaptationActions = adaptationActions;

            aa_db.aggregateAdaptationAction = ad.adaptationAction;

            return aa_db;
        }
    }
}
